i love you
you are my hero
i love you
I am probably late, but the best way to think about it is when you want to apply a function that could change depending on the situation it is best to use a callback function, as opposed to a hardcoded one.
The case with the function that verifies the run-time is a very good example when you analyze it. You have a function that receives any function and can verify the run-time of it. If you were to hard code every call to different functions you would have to rewrite the code every time you want to verify the run-time of a new function. Meanwhile with a higher-order function and callback functions you write your code once and just simply change the argument when you call the function.
Thanks! The explanations are greatly enhanced by adding some diagramming to the code. This was a pretty tough exercise to grok and your diagram helps clarify.
To gain a sense of appreciation for what HOFs represent, read up on pure functions that have no side effects. These are the functions most suited to the paradigm.
Once we reach this topic it is likely partnered up with the topic of iterators, which depend heavily on pure functions.
Really happy I wasn’t the only one to find the instructions poorly written. Hopefully, the additional material shared here will be useful!
To make this easier to understand you should do this…
copy and paste this line to change the const = addTwo()
const addTwo = num => num + Math.floor(Math.random() * 3)
this will produce a random number sometimes and give you the else fall back in the console.log sometimes and sometimes give you the output of the number that matched each time.
Then be sure to call the function like so…
Again if the number is the same then it will call it back in the console. If not it will give you the error string. I think they left the other function up top so you could do a callback function again on the addTwo. I understand the confusion.
So what’s the best way to understand these functions and really master them? I feel like i need Crayons.
Totally agree. This was very helpful. I actually switched from CA’s JS course to another school when I got to HOF. CA doesn’t teach it, they just talk about it in the abstract.
Hi, sorry if this has already been discussed but I couldnt see it anywhere…
I get the overall idea of this exercise (Functions as Parameters - 3/4).
However whats the deal with the Val and func words that just got thrown in out of nowhere? When the instructions said to put a function and value in for the parameters I was putting in the name of ACTUAL functions and value, since when can you just use these random keywords? What are they calling?
Please help, Im very confused.
Parameters are locally scoped, arbitrarily named variables that act for the actual values given to the function.
const foo = (func, val) => func(val); const bar = x => x ** 2 console.log(foo(bar, 2)) // 4
The step by step explanation and example is a mess… The worst lesson on CodeCademy by far. Should be redone ASAP.
That’s me right now :((( I understood and followed everything perfectly until this higher order functions lesson - I am so list right now!!! They should re-do this part - so many people find it so confusing, based on this discussion.
This section is very confusing, explanation doesn’t make sense, please improve it. Please report this @mtf
Excellent, This article´s helped me a lot!!
I totally agree with this. First time reading the exercise i did not understand what was the purpose of it. Not sure if i’m right, but as far as i understood, there are two completely different exercises there:
- the one with: checkThatTwoPlusTwoEqualsFourAMillionTimes , timeFuncRuntime and time2p2
- and a second one: addTwo, checkConsistentOutput
Correct me if i’m wrong, but it seems right, since i can delete either the first exercise or the second that everything works just fine, they are 100% independent. For a beginner that was so confusing.
Same actually, still don’t understand the bod function of the exercise. Why do 2 function calls and then an if statement to verify if it gets the same result twice?
Yes! They do seem independent! What is actually going on?
Sorry bro, still makes no sense to me. The exercise at least makes absolute 0 sense i terms of why did we need so many functions and they even seem independent from each other!
Didn’t write the course so cannot explain why this lesson is composed the way it is. We’re told to expect a rewrite sometime next year. Maybe this lesson will get sorted out, then.