Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!