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

141
Visualizações
Higher Order Functions and Callbacks - Using "return"
function eitherCallback(callback1, callback2) {
  return (element, i, array) => {return callback1(element, i, array) || callback2(element, i, array)}
}

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; // condition 1 as a function
const over100 = n => n > 100; // condition 2 as a function

const intSqRtOrOver100 = eitherCallback(integerSquareRoot, over100); // function to combine functions

console.log(filterArray(arrOfNums, intSqRtOrOver100)); // should log: [105, 9]

Above is a piece of code that I've written for my coding bootcamp prep. However, I am confused on the purpose of the second "return" in the eitherCallback function. I have read the syntax on arrow functions in ES6 and I read that the return is not needed if the function is a single line. However, when I remove that second "return" the output of console.log(filterArray(arrOfNums, intSqRtOrOver100)); returns an empty array.

The eitherCallback function takes two arguments: callback1 and callback2, which are going to be functions integerSquareRoot and over100. When I look at those functions, the return of those two functions are booleans which leads me to my confusion. Since callback1 and callback2 already return booleans, why do I get an empty array if I omit the second "return" in the eitherCallback function?

Thank you.

about 4 years ago · Juan Pablo Isaza
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