Higher order functions

I’m stuck on this exercise about higher functions. I’m on step 2 and I was trying to create an if…else statement, but the console displays that there is an error in my code as check A isn’t defined as an object. This is my code below and this is the link to the exercise https://www.codecademy.com/courses/introduction-to-javascript/lessons/higher-order-functions/exercises/functions-as-parameters

const addTwo = num => {
  return num + 2;

const checkConsistentOutput = (func, val) => {
const checkA = val + 2;
const checkB = func(val);
if (checkA === checkB) {
  } else {
    console.log('inconsistent results');

Also could someone explain the example on the exercise please? I don’t understand the explanation.

const higherOrderFunc = param => {
  return `I just invoked ${param.name} as a callback function!`
const anotherFunc = () => {
  return 'I\'m being invoked by the higher-order function!';

Try running this:

const higherOrderFunc = param => {
  // param(); <-- this would produce an error now
  return `I just tried to invoke ${param.name} as a callback function!` // your function does not have a property name 'name'
const anotherFunc = {
	name: 'an object'

That should work somehow and explain the problem.

1 Like

Thank you but that part was the example in the exercise. I wasn’t sure about the meaning of a callback function and the higher order function explanation in the example provided.

I was stuck on the first part of my code that I copied with the checkA checkB if…else statement.

You defined the variables CheckA and CheckB within the function scope of checkConsistentOutput. They aren’t available outside of that function. But in your if/else statement you are referencing these variables in the global scope.

1 Like

Ok so do I need another statement before the if/else statement?

You have to move your if/else statement inside the function scope.

I wasn’t able to get it right. The exercise solution shows the ternary operator version but I wanted to try it with an if/else statement, as the hint mentioned that it’s an option as well.

  • The if-else or ternary statement is supposed to be within the body of the checkConsistentOutput function. As mirja_t pointed out, checkA and checkB are within the scope of the function, but your if-else statement is outside the function at the global scope level. Therefore, checkA and checkB can’t be seen in the global scope. Just move the closing curly brace } of the function down to the bottom such that the if-else statement is also within the body of the function.

  • The instructions specify that you are supposed to return the result or the string 'inconsistent results'. You are instead logging them to the console. The console.log statement will come in step 3 right at the bottom (outside the function).


It worked, thank you.