diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.test.js b/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.jest.test.js similarity index 100% rename from src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.test.js rename to src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.jest.test.js diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.js b/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.js index 41264003..f5cbe76e 100644 --- a/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.js +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.js @@ -1,9 +1,7 @@ -function firstTeam() { +export function firstTeam() { // TODO } -function showStading() { +export function showStading() { // TODO } - -export { firstTeam, showStading }; diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.node.test.js b/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.node.test.js new file mode 100644 index 00000000..0ee394fc --- /dev/null +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/code/brazilian-championship.node.test.js @@ -0,0 +1,262 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { firstTeam, showStanding } from './brazilian-championship.js'; + +const standings = [ + { + team: 'Palmeiras', + mp: 38, + w: 24, + d: 8, + l: 6, + gf: 62, + ga: 32, + gd: 30, + pts: 80, + }, + { + team: 'Santos', + mp: 38, + w: 22, + d: 5, + l: 11, + gf: 59, + ga: 35, + gd: 24, + pts: 71, + }, + { + team: 'Flamengo', + mp: 38, + w: 20, + d: 11, + l: 7, + gf: 52, + ga: 35, + gd: 17, + pts: 71, + }, + { + team: 'Atlético Mineiro', + mp: 38, + w: 17, + d: 11, + l: 10, + gf: 61, + ga: 53, + gd: 8, + pts: 62, + }, + { + team: 'Botafogo', + mp: 38, + w: 17, + d: 8, + l: 13, + gf: 43, + ga: 39, + gd: 4, + pts: 59, + }, + { + team: 'Atlético-PR', + mp: 38, + w: 17, + d: 6, + l: 15, + gf: 38, + ga: 32, + gd: 6, + pts: 57, + }, + { + team: 'Corinthians', + mp: 38, + w: 15, + d: 10, + l: 13, + gf: 48, + ga: 42, + gd: 6, + pts: 55, + }, + { + team: 'Ponte Preta', + mp: 38, + w: 15, + d: 8, + l: 15, + gf: 48, + ga: 52, + gd: -4, + pts: 53, + }, + { + team: 'Grêmio', + mp: 38, + w: 14, + d: 11, + l: 13, + gf: 41, + ga: 44, + gd: -3, + pts: 53, + }, + { + team: 'São Paulo', + mp: 38, + w: 14, + d: 10, + l: 14, + gf: 44, + ga: 36, + gd: 8, + pts: 52, + }, + { + team: 'Chapecoense', + mp: 38, + w: 13, + d: 13, + l: 12, + gf: 49, + ga: 56, + gd: -7, + pts: 52, + }, + { + team: 'Cruzeiro', + mp: 38, + w: 14, + d: 9, + l: 15, + gf: 48, + ga: 49, + gd: -1, + pts: 51, + }, + { + team: 'Fluminense', + mp: 38, + w: 13, + d: 11, + l: 14, + gf: 45, + ga: 45, + gd: 0, + pts: 50, + }, + { + team: 'Sport Recife', + mp: 38, + w: 13, + d: 8, + l: 17, + gf: 49, + ga: 55, + gd: -6, + pts: 47, + }, + { + team: 'Coritiba', + mp: 38, + w: 11, + d: 13, + l: 14, + gf: 41, + ga: 42, + gd: -1, + pts: 46, + }, + { + team: 'Vitória', + mp: 38, + w: 12, + d: 9, + l: 17, + gf: 51, + ga: 53, + gd: -2, + pts: 45, + }, + { + team: 'Internacional', + mp: 38, + w: 11, + d: 10, + l: 17, + gf: 35, + ga: 41, + gd: -6, + pts: 43, + }, + { + team: 'Figueirense', + mp: 38, + w: 8, + d: 13, + l: 17, + gf: 30, + ga: 50, + gd: -20, + pts: 37, + }, + { + team: 'Santa Cruz', + mp: 38, + w: 8, + d: 7, + l: 23, + gf: 45, + ga: 69, + gd: -24, + pts: 31, + }, + { + team: 'América-MG', + mp: 38, + w: 7, + d: 7, + l: 24, + gf: 23, + ga: 58, + gd: -35, + pts: 28, + }, +]; + +describe('Brazilian Championship', () => { + describe('firstTeam()', () => { + it('should get the first team', () => { + assert.equal(firstTeam(standings), 'Palmeiras'); + }); + }); + + describe('showStanding()', () => { + it('should display standings', () => { + assert.equal( + showStanding(standings), + `Palmeiras | 38 | 24 | 8 | 6 | 62 | 32 | 30 | 80 +Santos | 38 | 22 | 5 | 11 | 59 | 35 | 24 | 71 +Flamengo | 38 | 20 | 11 | 7 | 52 | 35 | 17 | 71 +Atlético Mineiro | 38 | 17 | 11 | 10 | 61 | 53 | 8 | 62 +Botafogo | 38 | 17 | 8 | 13 | 43 | 39 | 4 | 59 +Atlético-PR | 38 | 17 | 6 | 15 | 38 | 32 | 6 | 57 +Corinthians | 38 | 15 | 10 | 13 | 48 | 42 | 6 | 55 +Ponte Preta | 38 | 15 | 8 | 15 | 48 | 52 | -4 | 53 +Grêmio | 38 | 14 | 11 | 13 | 41 | 44 | -3 | 53 +São Paulo | 38 | 14 | 10 | 14 | 44 | 36 | 8 | 52 +Chapecoense | 38 | 13 | 13 | 12 | 49 | 56 | -7 | 52 +Cruzeiro | 38 | 14 | 9 | 15 | 48 | 49 | -1 | 51 +Fluminense | 38 | 13 | 11 | 14 | 45 | 45 | 0 | 50 +Sport Recife | 38 | 13 | 8 | 17 | 49 | 55 | -6 | 47 +Coritiba | 38 | 11 | 13 | 14 | 41 | 42 | -1 | 46 +Vitória | 38 | 12 | 9 | 17 | 51 | 53 | -2 | 45 +Internacional | 38 | 11 | 10 | 17 | 35 | 41 | -6 | 43 +Figueirense | 38 | 8 | 13 | 17 | 30 | 50 | -20 | 37 +Santa Cruz | 38 | 8 | 7 | 23 | 45 | 69 | -24 | 31 +América-MG | 38 | 7 | 7 | 24 | 23 | 58 | -35 | 28` + ); + }); + }); +}); diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/code/package.json b/src/pages/exercises/object-brazilian-championship/_codes/js/code/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/code/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.test.js b/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.jest.test.js similarity index 100% rename from src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.test.js rename to src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.jest.test.js diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.js b/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.js index 01108a7c..bc91d624 100644 --- a/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.js +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.js @@ -1,12 +1,12 @@ -function firstTeam(standings) { +export function firstTeam(standings) { return standings.sort(sortTeam)[0].team; } -function showStading(standings) { +export function showStanding(standings) { return printTeams(standings.sort(sortTeam)); } -function sortTeam(a, b) { +export function sortTeam(a, b) { if (a.pts > b.pts) return -1; if (a.pts < b.pts) return 1; if (a.w > b.w) return -1; @@ -18,7 +18,7 @@ function sortTeam(a, b) { return 0; } -function printTeams(standings) { +export function printTeams(standings) { const maxLength = standings.reduce( (max, character) => { const teamLength = character.team.length; @@ -41,40 +41,22 @@ function printTeams(standings) { max.pts = ptsLength > max.pts ? ptsLength : max.pts; return max; }, - { team: 0, mp: 0, w: 0, d: 0, l: 0, gf: 0, ga: 0, gd: 0, pts: 0 }, + { team: 0, mp: 0, w: 0, d: 0, l: 0, gf: 0, ga: 0, gd: 0, pts: 0 } ); return standings - .map(row => { + .map((row) => { return [ row.team.padEnd(maxLength.team), - Number(row.mp) - .toString() - .padStart(maxLength.mp), - Number(row.w) - .toString() - .padStart(maxLength.w), - Number(row.d) - .toString() - .padStart(maxLength.d), - Number(row.l) - .toString() - .padStart(maxLength.l), - Number(row.gf) - .toString() - .padStart(maxLength.gf), - Number(row.ga) - .toString() - .padStart(maxLength.ga), - Number(row.gd) - .toString() - .padStart(maxLength.gd), - Number(row.pts) - .toString() - .padStart(maxLength.pts), + Number(row.mp).toString().padStart(maxLength.mp), + Number(row.w).toString().padStart(maxLength.w), + Number(row.d).toString().padStart(maxLength.d), + Number(row.l).toString().padStart(maxLength.l), + Number(row.gf).toString().padStart(maxLength.gf), + Number(row.ga).toString().padStart(maxLength.ga), + Number(row.gd).toString().padStart(maxLength.gd), + Number(row.pts).toString().padStart(maxLength.pts), ].join(' | '); }) .join('\n'); } - -export { firstTeam, showStading }; diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.node.test.js b/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.node.test.js new file mode 100644 index 00000000..0ee394fc --- /dev/null +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/response/brazilian-championship.node.test.js @@ -0,0 +1,262 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { firstTeam, showStanding } from './brazilian-championship.js'; + +const standings = [ + { + team: 'Palmeiras', + mp: 38, + w: 24, + d: 8, + l: 6, + gf: 62, + ga: 32, + gd: 30, + pts: 80, + }, + { + team: 'Santos', + mp: 38, + w: 22, + d: 5, + l: 11, + gf: 59, + ga: 35, + gd: 24, + pts: 71, + }, + { + team: 'Flamengo', + mp: 38, + w: 20, + d: 11, + l: 7, + gf: 52, + ga: 35, + gd: 17, + pts: 71, + }, + { + team: 'Atlético Mineiro', + mp: 38, + w: 17, + d: 11, + l: 10, + gf: 61, + ga: 53, + gd: 8, + pts: 62, + }, + { + team: 'Botafogo', + mp: 38, + w: 17, + d: 8, + l: 13, + gf: 43, + ga: 39, + gd: 4, + pts: 59, + }, + { + team: 'Atlético-PR', + mp: 38, + w: 17, + d: 6, + l: 15, + gf: 38, + ga: 32, + gd: 6, + pts: 57, + }, + { + team: 'Corinthians', + mp: 38, + w: 15, + d: 10, + l: 13, + gf: 48, + ga: 42, + gd: 6, + pts: 55, + }, + { + team: 'Ponte Preta', + mp: 38, + w: 15, + d: 8, + l: 15, + gf: 48, + ga: 52, + gd: -4, + pts: 53, + }, + { + team: 'Grêmio', + mp: 38, + w: 14, + d: 11, + l: 13, + gf: 41, + ga: 44, + gd: -3, + pts: 53, + }, + { + team: 'São Paulo', + mp: 38, + w: 14, + d: 10, + l: 14, + gf: 44, + ga: 36, + gd: 8, + pts: 52, + }, + { + team: 'Chapecoense', + mp: 38, + w: 13, + d: 13, + l: 12, + gf: 49, + ga: 56, + gd: -7, + pts: 52, + }, + { + team: 'Cruzeiro', + mp: 38, + w: 14, + d: 9, + l: 15, + gf: 48, + ga: 49, + gd: -1, + pts: 51, + }, + { + team: 'Fluminense', + mp: 38, + w: 13, + d: 11, + l: 14, + gf: 45, + ga: 45, + gd: 0, + pts: 50, + }, + { + team: 'Sport Recife', + mp: 38, + w: 13, + d: 8, + l: 17, + gf: 49, + ga: 55, + gd: -6, + pts: 47, + }, + { + team: 'Coritiba', + mp: 38, + w: 11, + d: 13, + l: 14, + gf: 41, + ga: 42, + gd: -1, + pts: 46, + }, + { + team: 'Vitória', + mp: 38, + w: 12, + d: 9, + l: 17, + gf: 51, + ga: 53, + gd: -2, + pts: 45, + }, + { + team: 'Internacional', + mp: 38, + w: 11, + d: 10, + l: 17, + gf: 35, + ga: 41, + gd: -6, + pts: 43, + }, + { + team: 'Figueirense', + mp: 38, + w: 8, + d: 13, + l: 17, + gf: 30, + ga: 50, + gd: -20, + pts: 37, + }, + { + team: 'Santa Cruz', + mp: 38, + w: 8, + d: 7, + l: 23, + gf: 45, + ga: 69, + gd: -24, + pts: 31, + }, + { + team: 'América-MG', + mp: 38, + w: 7, + d: 7, + l: 24, + gf: 23, + ga: 58, + gd: -35, + pts: 28, + }, +]; + +describe('Brazilian Championship', () => { + describe('firstTeam()', () => { + it('should get the first team', () => { + assert.equal(firstTeam(standings), 'Palmeiras'); + }); + }); + + describe('showStanding()', () => { + it('should display standings', () => { + assert.equal( + showStanding(standings), + `Palmeiras | 38 | 24 | 8 | 6 | 62 | 32 | 30 | 80 +Santos | 38 | 22 | 5 | 11 | 59 | 35 | 24 | 71 +Flamengo | 38 | 20 | 11 | 7 | 52 | 35 | 17 | 71 +Atlético Mineiro | 38 | 17 | 11 | 10 | 61 | 53 | 8 | 62 +Botafogo | 38 | 17 | 8 | 13 | 43 | 39 | 4 | 59 +Atlético-PR | 38 | 17 | 6 | 15 | 38 | 32 | 6 | 57 +Corinthians | 38 | 15 | 10 | 13 | 48 | 42 | 6 | 55 +Ponte Preta | 38 | 15 | 8 | 15 | 48 | 52 | -4 | 53 +Grêmio | 38 | 14 | 11 | 13 | 41 | 44 | -3 | 53 +São Paulo | 38 | 14 | 10 | 14 | 44 | 36 | 8 | 52 +Chapecoense | 38 | 13 | 13 | 12 | 49 | 56 | -7 | 52 +Cruzeiro | 38 | 14 | 9 | 15 | 48 | 49 | -1 | 51 +Fluminense | 38 | 13 | 11 | 14 | 45 | 45 | 0 | 50 +Sport Recife | 38 | 13 | 8 | 17 | 49 | 55 | -6 | 47 +Coritiba | 38 | 11 | 13 | 14 | 41 | 42 | -1 | 46 +Vitória | 38 | 12 | 9 | 17 | 51 | 53 | -2 | 45 +Internacional | 38 | 11 | 10 | 17 | 35 | 41 | -6 | 43 +Figueirense | 38 | 8 | 13 | 17 | 30 | 50 | -20 | 37 +Santa Cruz | 38 | 8 | 7 | 23 | 45 | 69 | -24 | 31 +América-MG | 38 | 7 | 7 | 24 | 23 | 58 | -35 | 28` + ); + }); + }); +}); diff --git a/src/pages/exercises/object-brazilian-championship/_codes/js/response/package.json b/src/pages/exercises/object-brazilian-championship/_codes/js/response/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-brazilian-championship/_codes/js/response/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-cep/_codes/js/code/cep.test.js b/src/pages/exercises/object-cep/_codes/js/code/cep.jest.test.js similarity index 100% rename from src/pages/exercises/object-cep/_codes/js/code/cep.test.js rename to src/pages/exercises/object-cep/_codes/js/code/cep.jest.test.js diff --git a/src/pages/exercises/object-cep/_codes/js/code/cep.js b/src/pages/exercises/object-cep/_codes/js/code/cep.js index aa0ade6c..0117a4e1 100644 --- a/src/pages/exercises/object-cep/_codes/js/code/cep.js +++ b/src/pages/exercises/object-cep/_codes/js/code/cep.js @@ -1,5 +1,3 @@ -function getInfo(cep) { +export function getInfo(cep) { // TODO } - -export { getInfo }; diff --git a/src/pages/exercises/object-cep/_codes/js/code/cep.node.test.js b/src/pages/exercises/object-cep/_codes/js/code/cep.node.test.js new file mode 100644 index 00000000..5ca14282 --- /dev/null +++ b/src/pages/exercises/object-cep/_codes/js/code/cep.node.test.js @@ -0,0 +1,34 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { getInfo } from './cep.js'; + +describe('CEP Util', () => { + describe('getInfo()', () => { + it('should display cep information', () => { + let expected = `cep: 01001-000, +logradouro: Praça da Sé, +complemento: lado ímpar, +bairro: Sé, +localidade: São Paulo, +uf: SP, +unidade: , +ibge: 3550308, +gia: 1004`; + + // https://viacep.com.br/ws/01001000/json/ + let cep = { + cep: '01001-000', + logradouro: 'Praça da Sé', + complemento: 'lado ímpar', + bairro: 'Sé', + localidade: 'São Paulo', + uf: 'SP', + unidade: '', + ibge: '3550308', + gia: '1004', + }; + + assert.equal(getInfo(cep), expected); + }); + }); +}); diff --git a/src/pages/exercises/object-cep/_codes/js/code/package.json b/src/pages/exercises/object-cep/_codes/js/code/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-cep/_codes/js/code/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-cep/_codes/js/response/cep.test.js b/src/pages/exercises/object-cep/_codes/js/response/cep.jest.test.js similarity index 100% rename from src/pages/exercises/object-cep/_codes/js/response/cep.test.js rename to src/pages/exercises/object-cep/_codes/js/response/cep.jest.test.js diff --git a/src/pages/exercises/object-cep/_codes/js/response/cep.js b/src/pages/exercises/object-cep/_codes/js/response/cep.js index cda83575..4d825e0a 100644 --- a/src/pages/exercises/object-cep/_codes/js/response/cep.js +++ b/src/pages/exercises/object-cep/_codes/js/response/cep.js @@ -1,7 +1,5 @@ -function getInfo(cep) { +export function getInfo(cep) { return Object.keys(cep) - .map(key => `${key}: ${cep[key]}`) + .map((key) => `${key}: ${cep[key]}`) .join(',\n'); } - -export { getInfo }; diff --git a/src/pages/exercises/object-cep/_codes/js/response/cep.node.test.js b/src/pages/exercises/object-cep/_codes/js/response/cep.node.test.js new file mode 100644 index 00000000..5ca14282 --- /dev/null +++ b/src/pages/exercises/object-cep/_codes/js/response/cep.node.test.js @@ -0,0 +1,34 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { getInfo } from './cep.js'; + +describe('CEP Util', () => { + describe('getInfo()', () => { + it('should display cep information', () => { + let expected = `cep: 01001-000, +logradouro: Praça da Sé, +complemento: lado ímpar, +bairro: Sé, +localidade: São Paulo, +uf: SP, +unidade: , +ibge: 3550308, +gia: 1004`; + + // https://viacep.com.br/ws/01001000/json/ + let cep = { + cep: '01001-000', + logradouro: 'Praça da Sé', + complemento: 'lado ímpar', + bairro: 'Sé', + localidade: 'São Paulo', + uf: 'SP', + unidade: '', + ibge: '3550308', + gia: '1004', + }; + + assert.equal(getInfo(cep), expected); + }); + }); +}); diff --git a/src/pages/exercises/object-cep/_codes/js/response/package.json b/src/pages/exercises/object-cep/_codes/js/response/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-cep/_codes/js/response/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-comics/_codes/js/code/comics.test.js b/src/pages/exercises/object-comics/_codes/js/code/comics.jest.test.js similarity index 100% rename from src/pages/exercises/object-comics/_codes/js/code/comics.test.js rename to src/pages/exercises/object-comics/_codes/js/code/comics.jest.test.js diff --git a/src/pages/exercises/object-comics/_codes/js/code/comics.js b/src/pages/exercises/object-comics/_codes/js/code/comics.js index e8c2082d..2ff29032 100644 --- a/src/pages/exercises/object-comics/_codes/js/code/comics.js +++ b/src/pages/exercises/object-comics/_codes/js/code/comics.js @@ -1,13 +1,11 @@ -function namesByComics(characters, comics) { +export function namesByComics(characters, comics) { // TODO } -function topWeightNames(characters, comics) { +export function topWeightNames(characters, comics) { // TODO } -function weight(characters, comics) { +export function weight(characters, comics) { // TODO } - -export { namesByComics, topWeightNames, weight }; diff --git a/src/pages/exercises/object-comics/_codes/js/code/comics.node.test.js b/src/pages/exercises/object-comics/_codes/js/code/comics.node.test.js new file mode 100644 index 00000000..c4cd2dd1 --- /dev/null +++ b/src/pages/exercises/object-comics/_codes/js/code/comics.node.test.js @@ -0,0 +1,83 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { namesByComics, topWeightNames, weight } from './comics.js'; + +const characters = [ + { + comics: 'Marvel', + height: 1.78, + link: [ + 'http://marvel.com/characters/54/spider-man', + 'http://marvel.com/universe/Spider-Man_(Peter_Parker)', + ], + name: 'Spider-Man', + realName: 'Peter Benjamin Parker', + weight: 76, + }, + { + comics: 'Marvel', + height: 2.44, + link: [ + 'http://marvel.com/characters/25/hulk', + 'http://marvel.com/universe/Hulk_(Bruce_Banner)', + ], + name: 'Hulk', + realName: 'Robert Bruce Banner', + weight: 635, + }, + { + comics: 'DC', + height: 1.91, + link: [ + 'http://www.dccomics.com/characters/superman', + 'http://dc.wikia.com/wiki/Superman_(Clark_Kent)', + ], + name: 'Superman', + realName: 'Clark Joseph Kent', + weight: 107, + }, + { + comics: 'DC', + height: 1.88, + link: [ + 'http://www.dccomics.com/characters/batman', + 'http://dc.wikia.com/wiki/Bruce_Wayne_(New_Earth)', + ], + name: 'batman', + realName: 'Bruce Wayne', + weight: 95, + }, + // TODO bad guys, villain +]; + +describe('Comics Tool', () => { + describe('namesByComics()', () => { + it('should list hero names by DC comics', () => { + assert.deepEqual(namesByComics(characters, 'DC'), ['batman', 'Superman']); + }); + }); + + describe('topWeightNames()', () => { + it('should sort heroes by weight', () => { + assert.deepEqual(topWeightNames(characters), [ + 'Hulk', + 'Superman', + 'batman', + 'Spider-Man', + ]); + }); + + it('should sort heroes by weight and comics', () => { + assert.deepEqual(topWeightNames(characters, 'Marvel'), [ + 'Hulk', + 'Spider-Man', + ]); + }); + }); + + describe('weight()', () => { + it('should calculate weight by comics', () => { + assert.equal(weight(characters, 'DC'), 101); + }); + }); +}); diff --git a/src/pages/exercises/object-comics/_codes/js/code/package.json b/src/pages/exercises/object-comics/_codes/js/code/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-comics/_codes/js/code/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-comics/_codes/js/response/comics.test.js b/src/pages/exercises/object-comics/_codes/js/response/comics.jest.test.js similarity index 100% rename from src/pages/exercises/object-comics/_codes/js/response/comics.test.js rename to src/pages/exercises/object-comics/_codes/js/response/comics.jest.test.js diff --git a/src/pages/exercises/object-comics/_codes/js/response/comics.js b/src/pages/exercises/object-comics/_codes/js/response/comics.js index d4ab945d..b7b62cdb 100644 --- a/src/pages/exercises/object-comics/_codes/js/response/comics.js +++ b/src/pages/exercises/object-comics/_codes/js/response/comics.js @@ -1,13 +1,15 @@ // Filters -const collectAllFilter = character => true; -const collectByComicsFilter = comics => character => character.comics === comics; -const funFilter = comics => (comics ? collectByComicsFilter(comics) : collectAllFilter); +const collectAllFilter = (character) => true; +const collectByComicsFilter = (comics) => (character) => + character.comics === comics; +const funFilter = (comics) => + comics ? collectByComicsFilter(comics) : collectAllFilter; // Reduces const sumWeight = (sum, character) => sum + character.weight; // Maps -const extractName = character => character.name; +const extractName = (character) => character.name; // Sorts const sortDesWeight = (a, b) => b.weight - a.weight; @@ -17,25 +19,22 @@ const sortAscLowerText = (a, b) => { else return 0; }; // Actions -function namesByComics(characters, comics) { +export function namesByComics(characters, comics) { return characters .filter(funFilter(comics)) .map(extractName) .sort(sortAscLowerText); } -function topWeightNames(characters, comics) { +export function topWeightNames(characters, comics) { return characters .filter(funFilter(comics)) .sort(sortDesWeight) .map(extractName); } -function weight(characters, comics) { +export function weight(characters, comics) { const charactersFilted = characters.filter(funFilter(comics)); const weightTotal = charactersFilted.reduce(sumWeight, 0); return weightTotal / charactersFilted.length; } - -// Export -export { namesByComics, topWeightNames, weight }; diff --git a/src/pages/exercises/object-comics/_codes/js/response/comics.node.test.js b/src/pages/exercises/object-comics/_codes/js/response/comics.node.test.js new file mode 100644 index 00000000..c4cd2dd1 --- /dev/null +++ b/src/pages/exercises/object-comics/_codes/js/response/comics.node.test.js @@ -0,0 +1,83 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import { namesByComics, topWeightNames, weight } from './comics.js'; + +const characters = [ + { + comics: 'Marvel', + height: 1.78, + link: [ + 'http://marvel.com/characters/54/spider-man', + 'http://marvel.com/universe/Spider-Man_(Peter_Parker)', + ], + name: 'Spider-Man', + realName: 'Peter Benjamin Parker', + weight: 76, + }, + { + comics: 'Marvel', + height: 2.44, + link: [ + 'http://marvel.com/characters/25/hulk', + 'http://marvel.com/universe/Hulk_(Bruce_Banner)', + ], + name: 'Hulk', + realName: 'Robert Bruce Banner', + weight: 635, + }, + { + comics: 'DC', + height: 1.91, + link: [ + 'http://www.dccomics.com/characters/superman', + 'http://dc.wikia.com/wiki/Superman_(Clark_Kent)', + ], + name: 'Superman', + realName: 'Clark Joseph Kent', + weight: 107, + }, + { + comics: 'DC', + height: 1.88, + link: [ + 'http://www.dccomics.com/characters/batman', + 'http://dc.wikia.com/wiki/Bruce_Wayne_(New_Earth)', + ], + name: 'batman', + realName: 'Bruce Wayne', + weight: 95, + }, + // TODO bad guys, villain +]; + +describe('Comics Tool', () => { + describe('namesByComics()', () => { + it('should list hero names by DC comics', () => { + assert.deepEqual(namesByComics(characters, 'DC'), ['batman', 'Superman']); + }); + }); + + describe('topWeightNames()', () => { + it('should sort heroes by weight', () => { + assert.deepEqual(topWeightNames(characters), [ + 'Hulk', + 'Superman', + 'batman', + 'Spider-Man', + ]); + }); + + it('should sort heroes by weight and comics', () => { + assert.deepEqual(topWeightNames(characters, 'Marvel'), [ + 'Hulk', + 'Spider-Man', + ]); + }); + }); + + describe('weight()', () => { + it('should calculate weight by comics', () => { + assert.equal(weight(characters, 'DC'), 101); + }); + }); +}); diff --git a/src/pages/exercises/object-comics/_codes/js/response/package.json b/src/pages/exercises/object-comics/_codes/js/response/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-comics/_codes/js/response/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-exam/_codes/js/code/exam-check.js b/src/pages/exercises/object-exam/_codes/js/code/exam-check.js deleted file mode 100644 index ac25fa51..00000000 --- a/src/pages/exercises/object-exam/_codes/js/code/exam-check.js +++ /dev/null @@ -1,5 +0,0 @@ -class ExamCheck { - // TODO -} - -export { ExamCheck }; diff --git a/src/pages/exercises/object-exam/_codes/js/code/exam-stats.js b/src/pages/exercises/object-exam/_codes/js/code/exam-stats.js deleted file mode 100644 index 310fd9c8..00000000 --- a/src/pages/exercises/object-exam/_codes/js/code/exam-stats.js +++ /dev/null @@ -1,5 +0,0 @@ -class ExamStats { - // TODO -} - -export { ExamStats }; diff --git a/src/pages/exercises/object-exam/_codes/js/code/exam.test.js b/src/pages/exercises/object-exam/_codes/js/code/exam.jest.test.js similarity index 69% rename from src/pages/exercises/object-exam/_codes/js/code/exam.test.js rename to src/pages/exercises/object-exam/_codes/js/code/exam.jest.test.js index 69d7aaf5..3c270b35 100644 --- a/src/pages/exercises/object-exam/_codes/js/code/exam.test.js +++ b/src/pages/exercises/object-exam/_codes/js/code/exam.jest.test.js @@ -1,5 +1,4 @@ -import { ExamCheck } from './exam-check.js'; -import { ExamStats } from './exam-stats.js'; +import { Exam } from './exam-stats.js'; describe('Exam Tool', () => { test('calculating grade by weighted average', () => { @@ -21,21 +20,21 @@ describe('Exam Tool', () => { }); test.skip('calculating exam values', () => { - let examStats = new ExamStats([ + let exam = new Exam([ { student: 'Fulano', grade: 10 }, { student: 'Sicrano', grade: 5 }, { student: 'Beltrano', grade: 7 }, ]); - expect(examStats.avg()).toBe(7.333333333333333); + expect(exam.avg()).toBe(7.333333333333333); - expect(examStats.min()).toEqual([5]); - expect(examStats.min(2)).toEqual([5, 7]); + expect(exam.min()).toEqual([5]); + expect(exam.min(2)).toEqual([5, 7]); - expect(examStats.max()).toEqual([10]); + expect(exam.max()).toEqual([10]); - expect(examStats.lt(6)).toEqual([5]); + expect(exam.lt(6)).toEqual([5]); - expect(examStats.gt(6)).toEqual([7, 10]); + expect(exam.gt(6)).toEqual([7, 10]); }); }); diff --git a/src/pages/exercises/object-exam/_codes/js/code/exam.js b/src/pages/exercises/object-exam/_codes/js/code/exam.js new file mode 100644 index 00000000..45e494af --- /dev/null +++ b/src/pages/exercises/object-exam/_codes/js/code/exam.js @@ -0,0 +1,3 @@ +export class ExamStats { + // TODO +} diff --git a/src/pages/exercises/object-exam/_codes/js/code/exam.node.test.js b/src/pages/exercises/object-exam/_codes/js/code/exam.node.test.js new file mode 100644 index 00000000..17cb610e --- /dev/null +++ b/src/pages/exercises/object-exam/_codes/js/code/exam.node.test.js @@ -0,0 +1,111 @@ +import { describe, it, beforeEach } from 'node:test'; +import assert from 'node:assert'; +import { Exam } from './exam.js'; + +describe('Exam Tool', () => { + let exam; + + beforeEach(() => { + const weight = { q1: 2, q2: 2, q3: 2, q4: 2, q5: 2 }; + + const answer = { q1: 'a', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }; + + exam = new Exam(answer, weight); + }); + + it('should calculate average', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.equal(exam.avg(), 4); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.equal(exam.avg(), 6); + }); + + it('should calculate minimum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.min(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.min(), [4]); + }); + + it('should calculate minimum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.min(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.min(), [4]); + }); + + it('should calculate maximum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.max(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.max(), [8]); + }); + + it('should calculate the values is less than a value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.lt(7), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.lt(7), [4]); + }); + + it('should calculate the values is greater than a value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.gt(7), []); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.gt(7), [8]); + }); +}); diff --git a/src/pages/exercises/object-exam/_codes/js/code/package.json b/src/pages/exercises/object-exam/_codes/js/code/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-exam/_codes/js/code/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/src/pages/exercises/object-exam/_codes/js/response/exam.test.js b/src/pages/exercises/object-exam/_codes/js/response/exam.jest.test.js similarity index 100% rename from src/pages/exercises/object-exam/_codes/js/response/exam.test.js rename to src/pages/exercises/object-exam/_codes/js/response/exam.jest.test.js diff --git a/src/pages/exercises/object-exam/_codes/js/response/exam.js b/src/pages/exercises/object-exam/_codes/js/response/exam.js index 374575f9..0535c676 100644 --- a/src/pages/exercises/object-exam/_codes/js/response/exam.js +++ b/src/pages/exercises/object-exam/_codes/js/response/exam.js @@ -1,4 +1,4 @@ -class Exam { +export class Exam { constructor(answer, weight) { this.weight = weight; this.answer = answer; @@ -54,5 +54,3 @@ class Exam { return grades.filter((grade) => grade > limit); } } - -export { Exam }; diff --git a/src/pages/exercises/object-exam/_codes/js/response/exam.node.test.js b/src/pages/exercises/object-exam/_codes/js/response/exam.node.test.js new file mode 100644 index 00000000..17cb610e --- /dev/null +++ b/src/pages/exercises/object-exam/_codes/js/response/exam.node.test.js @@ -0,0 +1,111 @@ +import { describe, it, beforeEach } from 'node:test'; +import assert from 'node:assert'; +import { Exam } from './exam.js'; + +describe('Exam Tool', () => { + let exam; + + beforeEach(() => { + const weight = { q1: 2, q2: 2, q3: 2, q4: 2, q5: 2 }; + + const answer = { q1: 'a', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }; + + exam = new Exam(answer, weight); + }); + + it('should calculate average', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.equal(exam.avg(), 4); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.equal(exam.avg(), 6); + }); + + it('should calculate minimum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.min(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.min(), [4]); + }); + + it('should calculate minimum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.min(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.min(), [4]); + }); + + it('should calculate maximum value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.max(), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.max(), [8]); + }); + + it('should calculate the values is less than a value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.lt(7), [4]); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.lt(7), [4]); + }); + + it('should calculate the values is greater than a value', () => { + exam.add({ + student: 'Alice', + answer: { q1: 'a', q2: 'b', q3: 'b', q4: 'b', q5: 'b' }, + }); + + assert.deepEqual(exam.gt(7), []); + + exam.add({ + student: 'Bob', + answer: { q1: 'c', q2: 'b', q3: 'a', q4: 'c', q5: 'd' }, + }); + + assert.deepEqual(exam.gt(7), [8]); + }); +}); diff --git a/src/pages/exercises/object-exam/_codes/js/response/package.json b/src/pages/exercises/object-exam/_codes/js/response/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/src/pages/exercises/object-exam/_codes/js/response/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +}