Given Code Gives Error?

So im learning about higher order function i have a good idea on it but started to think maybe i was taught wrong. When i got to this lesson the following code is the code given off the bat. When running the code i get that ‘funcParameter’ is not a function…well then 1. why was i shown to do this and 2. Why is it in the initial code?..so i removed it and was able to complete step one and two without getting an error. but step 3 has me write my own higher order code i wrote it, got an error and checked the hint which gave me exactly what i wrote which will be shown in code 2 which also gave me ‘aFunc is not a function’. At this point i dont understand what or if im doing wrong. Any ideas? if im not wrong then whats the right way?

const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
  for(let i = 1; i <= 1000000; i++) {
    if ( (2 + 2) != 4) {
      console.log('Something has gone very wrong :( ');
    }
  }
};

const addTwo = num => num + 2;

const timeFuncRuntime = funcParameter => {
  let t1 = Date.now();
  funcParameter();
  let t2 = Date.now();
  return t2 - t1;
};
let time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes());

const checkConsistentOutput = (aFunc, a) => {
  let firstTime = aFunc(a);
  let secondTime = aFunc(a);
  if (firstTime === secondTime) {
    return firstTime;
  } else {
    return 'This function returned inconsistent results'
  }
}

checkConsistentOutput(addTwo(3));

Hello @guccitoiletttv, welcome to the forums! Can you post a link to the exercise, please?

Thanks! And absolutely, here it is ===> [https://www.codecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-higher-order-functions-and-iterators/modules/game-dev-learn-javascript-iterators/lessons/higher-order-functions/exercises/functions-as-parameters]

It seems that the code isn’t meant to work before you write anything; you need to call the timeFuncRuntime function. When you pass a function in as an argument, you don’t need the parentheses after it.

oh i actually just realize when i called the time2p2 i did () at the end of the argument, but why does it cause the console to say that funcPerameter is not a function but doesnt do that when i call without the ()?

Because when you add parentheses, you’re actually passing in the return value of the function, rather than the function itself. And in this case, since the return value is undefined, which isn’t a function, the error is thrown.

ohhhhhhhh. makes so much sense. Thank you for the help!

1 Like

No problem; happy to help!