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

248
Vistas
Combining 2 Callback Functions into One

Here is the question...

"Add code to the function eitherCallback in the place marked "ADD CODE HERE" in order to achieve the desired console logs. The result of using eitherCallback to combine two callbacks into one callback and then passing that one callback into filterArray should match the results of simply passing the two callbacks into eitherFilter in the previous challenge."

Here is the previous challenge's solution which I know works...

function eitherFilter(array, callback1, callback2) {
  // ADD CODE HERE
  const newArr = [];
  for (let i = 0; i < array.length; i++) {
    if (callback1(array[i]) || callback2(array[i])) {
      newArr.push(array[i]);
    }
  }
  return newArr;
}

// Uncomment these to check your work!
 const arrOfNums = [10, 35, 105, 9];
 const integerSquareRoot = n => Math.sqrt(n) % 1 === 0;
 const over100 = n => n > 100;
 console.log(eitherFilter(arrofNums, integerSquareRoot, over100)); // should log: [105, 9]

Here is the code given...

function eitherCallback(callback1, callback2) {
  // ADD CODE HERE
}

// Uncomment these to check your work!
 function filterArray(array, callback) {
   const newArray = [];
   for (let i = 0; i < array.length; i += 1) {
     if (callback(array[i], i, array)) newArray.push(array[i]);
   }
   return newArray;
 }
 const arrOfNums = [10, 35, 105, 9];
 const integerSquareRoot = n => Math.sqrt(n) % 1 === 0;
 const over100 = n => n > 100;
 const intSqRtOrOver100 = eitherCallback(integerSquareRoot, over100);
 console.log(filterArray(arrOfNums, intSqRtOver100)); // should log: [105, 9]

I am confused as to what to do. Can anyone give me some tips? I do not know how to even start to answer it!

Thanks in advance...

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

0

You don't need much - all you need is to make eitherCallback a higher-order function that takes the two callbacks as initial argument, and executes and returns the logic you're carrying out here:

callback1(array[i]) || callback2(array[i])

Like:

const eitherCallback = (callback1, callback2) => x => callback1(x) || callback2(x);

// Uncomment these to check your work!
 function filterArray(array, callback) {
   const newArray = [];
   for (let i = 0; i < array.length; i += 1) {
     if (callback(array[i], i, array)) newArray.push(array[i]);
   }
   return newArray;
 }
 const arrOfNums = [10, 35, 105, 9];
 const integerSquareRoot = n => Math.sqrt(n) % 1 === 0;
 const over100 = n => n > 100;
 const intSqRtOrOver100 = eitherCallback(integerSquareRoot, over100);
 console.log(filterArray(arrOfNums, intSqRtOrOver100)); // should log: [105, 9]

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could also try:

function eitherCallback(callback1, callback2) {
  // ADD CODE HERE
  return (element, i, array) => {
   //element representing array[i] 
    return callback1(element, i, array) || callback2(element, i, array);
  }
}

The idea is for eitherCallback to return a truly value for either callback called within the function. Also, I believe the "x" in the previous solution from certain performance represents the arguments array[i], i and array. It's just a cleaner way to write it out with x instead of having to repeat it all. Keeping things DRY.

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