Codecademy Forums

Higher order functions

Hi, either this exercise is astonishingly bad, or I’m astonishingly bad, possibly a mixture of both, but I’ve been trying work out what it’s trying to tell us to do for at least a couple of hours. First up, I just cannot see the relevance of funcParameter() or this line of code: const addOneToOne = () => 1 + 1; in the code below (that they give as an example):

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

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

They don’t actually seem to have any purpose. Then, every exercise, hint and error message seems to have very little relevance to actually teaching us practically what callback functions are for or how they work. I have grasped the concept which is what is says in the second line of text at the top of the introduction which is: we can also pass functions (into other functions) as parameters.

Ok I’m new to programming but it’s doing a terrible job of educating me because suddenly I just hit a huge brick wall with codeacademy. If I didn’t have a tech interview for a bootcamp next week this would probably be the stage at which I give up with CA.

Ok update, it wasn’t until I hacked my way to the end of this exercise with cheats that I even realised the different sections of code were even connected :frowning:

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;
};

// Write your code below

const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

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

checkConsistentOutput(addTwo, 10);
2 Likes

this is just a difficult concept, with an example which is indeed not helping very much.

what exactly is a higher order function? It is explained:

A higher-order function is a function that either accepts functions as parameters, returns a function, or both!

this makes timeFuncRuntime a higher order function, because it accepts a function as parameter. However, at this point, the parameter accepts all types. We can pass strings, integers, float and so on, so it would be good to build in a check:

const timeFuncRuntime = funcParameter => {
   if (typeof funcParameter !== "function") return

so, now our function only accepts functions.

then there is this bit:

When we pass a function in as an argument to another function, we don’t invoke it. Invoking the function would evaluate to the return value of that function call. With callbacks, we pass in the function itself by typing the function name without the parentheses (that would evaluate to the result of calling the function

which corresponds with this line:

timeFuncRuntime(addOneToOne);

we do not invoke addOneToOne, do you see this?

finally:

We wrote a higher-order function, timeFuncRuntime() . It takes in a function as an argument, saves a starting time, invokes the callback function, records the time after the function was called, and returns the time the function took to run by subtracting the starting time from the ending time.

the tricky bit is invoking the callback function, which is thankfully explained:

We call the functions that get passed in as parameters and invoked callback functions because they get called during the execution of the higher-order function

which all refers to this line:

funcParameter();

addOneToOne function (callback function) gets invoked in the timeFuncRuntime function (higher order function)

Higher order function offer abstraction, which has advantages, but not this early in the learning JS process i think.

1 Like

Thanks very much for your detailed response. I’m determined to crack it so I’ll go back and try and work it out. For anyone else I found this video really descriptive and easy to understand:

1 Like

Just to add further, I’ve shared this with some developer friends who think this sucks and that there might also be bugs in it. One said that step 3 wouldn’t complete unless you load the hint. No one really liked it and one of them pointed to “The Odin Project” for a much better way of learning this.

When I was stuck on step three, I checked off my code against the complete code hint for about 20 minutes, but it just would not work unless I copy and pasted the hint.

Unhappy.

2 Likes

Thanks for sharing this video! For the first time in many days the ‘higher order function’ concepts makes sense! I still have no clue what the codecademy example accomplishes (i.e what does 1+1 have to do with picking two points in time and subtracting one from the other??). I completely understand the ‘calc’ function example in the Youtube video.
I also agree with everything I’ve read thus far in the community forum that the course example timeFuncRuntime is the worst ever and contributes nothing to the understanding of this key concept. Rather, it adds a lot of confusion and frustration. Like you, it drove me to other sources to better understand it, including Youtube and Treehouse.

I agree, a much better example would be to use filter:

const myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenList = myList.filter(element => element % 2 === 0 );
console.log(evenList);

and then implement the filter method yourself, to see why higher order function can be really useful (saves code + time)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.