Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda