-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.js
50 lines (44 loc) · 1.45 KB
/
01.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
https://adventjs.dev/challenges/01
Considera una lista/array de ovejas. Cada oveja tiene un nombre y un color.
Haz una función que devuelva una lista con todas las ovejas que sean de color
rojo y que además su nombre contenga tanto las letras n Y a,
sin importar el orden, las mayúsculas o espacios.
Por ejemplo, si tenemos las ovejas:
const ovejas = [
{ name: 'Noa', color: 'azul' },
{ name: 'Euge', color: 'rojo' },
{ name: 'Navidad', color: 'rojo' },
{ name: 'Ki Na Ma', color: 'rojo'},
{ name: 'AAAAAaaaaa', color: 'rojo' },
{ name: 'Nnnnnnnn', color: 'rojo'}
]
Al ejecutar el método debería devolver lo siguiente:
const ovejasFiltradas = contarOvejas(ovejas)
console.log(ovejasFiltradas)
// [{ name: 'Navidad', color: 'rojo' },
// { name: 'Ki Na Ma', color: 'rojo' }]
Recuerda. Debe contener las dos letras 'a' y 'n' en el nombre. No cuentes ovejas
que sólo tenga una de las letras, debe tener ambas.
*/
const ovejas = [
{ name: 'Noa', color: 'azul' },
{ name: 'Euge', color: 'rojo' },
{ name: 'Navidad', color: 'rojo' },
{ name: 'Ki Na Ma', color: 'rojo' },
{ name: 'AAAAAaaaaa', color: 'rojo' },
{ name: 'Nnnnnnnn', color: 'rojo' },
]
function contarOvejas(ovejas) {
return ovejas.reduce((acc, curr) => {
if (
curr.color === 'rojo' &&
curr.name.toLowerCase().includes('a') &&
curr.name.toLowerCase().includes('n')
)
acc.push(curr)
return acc
}, [])
}
console.clear()
console.log(contarOvejas(ovejas))