This is the array:
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'}
]
And he asks me to filter the red sheep and that their name also contain the letters n and a. The base code they give me is the following:
export default function contarOvejas(ovejas) {
// aquí tu magia
return ovejas
}
const ovejasFiltradas = contarOvejas(ovejas)
console.log(ovejasFiltradas)
// [{ name: 'Navidad', color: 'rojo' },
// { name: 'Ki Na Ma', color: 'rojo' }]
My code does something similar but not exactly what it asks for, also if I return from one (as the exercise asks) it only prints to the console when I call the function, the first match and the execution is interrupted. Can you help me..? Thank you
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'}
]
const contarOvejas = (lista) => {
for (let oveja of ovejas) {
let reg = new RegExp(/a/i);
let reg2 = new RegExp(/n/i);
if (
oveja.color === 'rojo' &&
reg.test(oveja.name) &&
reg2.test(oveja.name)
) {
console.log(oveja);
}
}
};
contarOvejas(ovejas);
I don't know if you mean this:
where what we do is create an array inside the function, which I have called output , to which the matches found in the conditional if are added through the push method , and finally we return that array through return at the end of the function.