Skip to content

Commit c051468

Browse files
committed
✨ challenge-02 solution added
1 parent 0d9615c commit c051468

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Reto 2: ¡Ayuda al elfo a listar los regalos!
2+
3+
## Problema
4+
5+
Te ha llegado una carta ✉️ con todos los regalos que debes preparar. El tema es que es una cadena de texto y es muy difícil de leer 😱. ¡Menos mal que han puesto cada regalo separado por espacio! (aunque ten cuidado, porque al ser niños, igual han colado más espacios de la cuenta)
6+
7+
Encima nos hemos dado cuenta que algunas palabras vienen con un \_ delante de la palabra, por ejemplo \_playstation, que significa que está tachado y no se tiene que contar.
8+
9+
Transforma el texto a un objeto que contenga el nombre de cada regalo y las veces que aparece. Por ejemplo, si tenemos el texto:
10+
11+
```js
12+
const carta = "bici coche balón _playstation bici coche peluche";
13+
```
14+
15+
Al ejecutar el método debería devolver lo siguiente:
16+
17+
```js
18+
const regalos = listGifts(carta);
19+
20+
console.log(regalos);
21+
/*
22+
{
23+
bici: 2,
24+
coche: 2,
25+
balón: 1,
26+
peluche: 1
27+
}
28+
*/
29+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const listGifts = (letter) => letter.trim().split(' ').reduce((acc, gift) => {
2+
if (!gift.startsWith('_')) {
3+
acc[gift] = (acc[gift] || 0) + 1;
4+
}
5+
return acc;
6+
}, {});
7+
8+
module.exports = listGifts;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const listGifts = require('./index');
2+
3+
describe('02 => Ayuda al elfo a listar los regalos', () => {
4+
const testCases = [
5+
{
6+
input: 'bici coche balón _playstation bici coche peluche',
7+
output: {
8+
bici: 2, coche: 2, balón: 1, peluche: 1,
9+
},
10+
},
11+
{
12+
input: 'bici coche balón _playstation bici coche peluche bici coche balón _playstation bici coche peluche',
13+
output: {
14+
bici: 4, coche: 4, balón: 2, peluche: 2,
15+
},
16+
},
17+
];
18+
19+
it('should return an object type', () => {
20+
expect(typeof listGifts('bici coche balón _playstation bici coche peluche')).toBe('object');
21+
});
22+
23+
it.each(testCases)('should return an object with the correct values', (testCase) => {
24+
expect(listGifts(testCase.input)).toEqual(testCase.output);
25+
});
26+
});

0 commit comments

Comments
 (0)