FAQ: Higher-Order Functions - Review


#1

This community-built FAQ covers the “Review” exercise from the lesson “Higher-Order Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I’m a bit confused by this lesson. I didn’t see any results in the console, so I wasn’t sure exactly what the code was doing, or trying to do. When I tried to print the variables to the console log, I received an error message.
Also, In the last exercise, when I typed "checkConsistentOutput(addTwo, 2);’ I got an error - but the solution was checkConsistentOutput(addTwo, 10);. What did this check, and why did 10 work and not 2? Why wouldn’t it let me print the answer to the console?


#3

I’m having the same issue. If anyone has the correct solution, would you please post it? I can’t get the solution button to appear again.


#4

This is what I had for the last question and it worked, but still I’m a bit confused over that whole lesson. Might take me a bit to really understand higher-order functions. Especially the anonymous arguments part…

function checkConsistentOutput(func, value) {
let first = func(value);
let second = func(value);
if (first === second) {
return first;
} else {
return “This function returned inconsistent results”;
}
}
console.log(checkConsistentOutput(addTwo, 5));


#5

If anybody still has problems understanding high order functions and callbacks watch this video. Definitely cleared up a lot for me!


#6

As I understand it, the following code actually invokes the function ‘checkConsistentOutput()’ and passes in the callback function ‘addTwo()’ as well as a value (in this case 20, though I experimented with many values and they all worked):

checkConsistentOutput(addTwo, 20);

Inside the ‘function body’ of ‘checkConsistentOutput()’, the ‘addTwo()’ function is invoked twice (in the first two lines of the 'function body) and passed the value (in this case, 20):

const checkConsistentOutput = (func, val) => {
  let firstTry = func(val);
  let secondTry = func(val);
  if (firstTry === secondTry) {
    return firstTry;
  } else {
    return 'This function returned inconsistent results';
  }
};

That passed the value 20 into the ‘addTwo’ function, performing ‘num + 2’ (in my case 20 + 2… resulting in 22):

const addTwo = num => num + 2;

The return value of 22 was passed back to checkConsistentOutput(), compared in firstTry and secondTry, found to be equal (’===’) and returned. I logged it to the console to see the result in this manner:

console.log(checkConsistentOutput(addTwo, 20));

Hope that helps!