Better understanding higher order functions - helpful resources

Just some thoughts about this exercise - from


These two articles help me to understand better the exercise

43 Likes

Many Many Thanks but you know still hard, it is so complicated.

25 Likes

So true. It’s like there was a huge leap in this exercise from Arrays to Loops. Now we’re in a Brave New World.
I’ve been rolling with the things I didn’t understand until now. Now I feel like I need to sit down and examine the elements in Javascript to understand the layout.

44 Likes

Instructions should be rewritten. HOF are complicated but they aren’t that abstract, they are enough for any human to understand. Problem is, the instructions.

45 Likes

I have to agree on this. I understand that functions can be passed as parameters, that’s not the difficulty. What I struggle to understand is how this kind of callback is useful when used. I’m supposed to learn an advanced concept but I have no clue what its purpose is. If that was clarified with simple examples I think it would then just be a matter of learning the syntax properly.

25 Likes

I’m no way near understanding this, but found this a really helpful read.

20 Likes

Totally agree, the instructions are very poorly written. It would help to understand what the objective actually is

8 Likes

I’m completely stuck on this lesson. The explanation is hard to understand. I’m trying to find the info on other resources but with no success yet. It would be great if your examples will relate to real life applications. Even though I somehow completed the lesson I feel that I didn’t grasp the concept in order to apply it in my future projects.

29 Likes

Was totally confused by this lesson, I feel the instructions didn’t really explain what was going on, and by the time I got to instruction 2, it made absolutely 0 sense to me, in terms of what it as asking for.
When looking at the hint, I didn’t understand how I was supposed to figure past -

const checkConsistentOutput = (func, val) => {

Very difficult lesson to grasp.

27 Likes

Thank you for this, this article was a great help.

1 Like

Thank you for the articles, they are definitely very helpful

1 Like

I want to provide an article that helped me understand HOF better. Hopefully, it’d help you readers as well:

Understanding Higher-Order Functions in JavaScript[]

4 Likes

Totally agree with you, syntax and use are not that ■■■■, but at this level (absolute beginner) it’s hard to figure out when and where to use them…

1 Like

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

const addTwo = num => num + 2;

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

// Write your code below

let checkConsistentOutput=(func,val)=>{
let first=func(val)
let second=func(val)
if(first===second){
return first
}else{
return ‘This function returned inconsistent results’
}
}
checkConsistentOutput(addTwo,1)

console.log(checkConsistentOutput)

I am completely lost here. Can someone help to break this down…

If you log this line, it will print the result.

That line is only logging [Function] or something similar. It is only a reference to the function, not an invocation of it, as with the above.

2 Likes

Sorry for the delay. I had the same problem. In the following link you can find a complete explanation for the problem with that exact code and its solution:

https://discuss.codecademy.com/t/higher-order-functions/395424

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);
1 Like

Hi mft,

Hope is all well and a Happy New Year as we are almost there.

I am trying to understand the Higher Order Functions concept but I find myself extremely confused by the definitions.
The Introduction gives the following definition:
» Higher-order functions are functions that accept other functions as arguments and/or return functions as output.
I contrast to the definition from Functions as Parameters :
» A higher-order function is a function that either accepts functions as parameters, returns a function, or both.
In summary (according to above) HOFs are functions that can take other functions as arguments, parameters, and/or return functions as outputs, is the statement correct ?

Many thanks for your time in providing an answer.

The simple difference between argument and parameter is that one is a value or object reference, the other is a variable to refer to the argument by within the scope of the function.

function foo (parameter) {
    // code
}

foo(argument)    // function call (invoke with an argument)

I’d be inclined to focus on the first definition, without letting the second one alter the meaning, however similar or connected.

calc = (func, a, b) => func(a, b)

The above is a function that takes a function and two numeric arguments and returns the numbers as computed by the function.

console.log(calc((a, b) => a * b, 6, 7))    // 42

Give this a try and post again if we have not answered your question. This deserves a measure of discussion and a few more examples.

2 Likes

Hi;
I need a little basic explanation regarding to exercise 2 in 3/4.
This is what was written:
const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry;
The question is, what is the “func”, how it turn to function?
and second question is;
in one of other teacher exercises he wrote:
var showName = function() {
var name = “FP JavaScript”
return function() {
console.log(name)
}
}()
showName() //==>> FP JavaScript
console.log(name) //==>> Reference Error
I mean the last two lines, when I use:
checkConsistentOutput();
to give me the result, all page get red lines of error. What is wrong?
More than solution I need the concept.
gratefully

LOL good luck understanding now