Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
Filtering Arrays in JavaScript using Callback Functions

The question is:

"Add code to the functions func1 and func2 in the places marked "ADD CODE HERE" in order to achieve the desired console logs."

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (callback(array[i])) newArray.push(array[i]);
  }
  return newArray;
}
const arrOfNums = [1, 2, 3, 4, 5];
function func1(num) {
  // ADD CODE HERE

}
function func2(num) {
  // ADD CODE HERE

}

 console.log(filterArray(arrOfNums, func1)); // should log: [2, 4]
 console.log(filterArray(arrOfNums, func2)); // should log: [1, 3, 5]

In func1 I tried:

  if (num % 2 === 0) {
    num = num;
  } else {
    num = null;
  }

In func2 I tried:

  if (num % 2 !== 0) {
    num = num;
  } else {
    num = null;
  }

I am unsure how else to approach this problem. My solution did not work and the console.log calls both returned empty arrays...

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The callback function need to give back a boolean value, if true then the filterArray function addig the element to the array.

if (callback(array[i])) newArray.push(array[i]);

So this line only calling the newArray.push(array[i]) if the callback(array[i]) return value is true.

Solution:

function func1(num) {  
   return (num % 2 === 0);
}

function func2(num) {
   return (num % 2 !== 0);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your functions don't return anything but the callback functions for filter have to return a boolean for each value.

You can use

return num % 2 === 0;

resp.

return num % 2 !== 0;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to return true/ false

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (callback(array[i])) newArray.push(array[i]);
  }
  return newArray;
}
const arrOfNums = [1, 2, 3, 4, 5];
function func1(num) {
  // return num % 2 === 0;
  if (num % 2 === 0) {
   return true;
  } else {
    return false;
  }

}
function func2(num) {
  // return num % 2 !== 0;
  if (num % 2 !== 0) {
    return true;
  } else {
    return false;
  }

}

 console.log(filterArray(arrOfNums, func1)); // should log: [2, 4]
 console.log(filterArray(arrOfNums, func2)); // should log: [1, 3, 5]

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda