SImple question about a function question on the quiz

Hey guys, in the quiz we had to assign a value to the value variable, in this case the correct answer is func(3, 5).
I don´t understand how this correct, because we have added two parameters to the value, which I suppose pass on to the parameter of the hof function.
It seems like an inefficeint way of doing this, when the parameters could just be included directly to the hof function.

Any explanation would be great!! I´m sure it is simple, I just cant see it.

let cb = (n1, n2) => {return n1 + n2};

let hof = (func) => {
let value = func(3, 5);
return this function returned ${value};
}

hof(cb) // ‘this function returned 8’

Not sure, I understand correctly, but what you’re asking is why we don’t write it like this:
let cb = () => {return 3 + 5};,
right?

The purpose of a function is to be generic. Think of a use case for a function like this. If you just want to know what 3 + 5 is , you could either just take a calculator or do it in your head. But you could also link the function to a website where the visitor could type whatever number they want to sum up. Then you need to pass the input to the function as parameters, because you don’t know in advance what they’ll type.

In case I got your question wrong, it would be helpful if you upload a screenshot of the question.

Hi Mirja!!!
Thanks for this :slight_smile: I try and explain myself… I appreciate your time, if I can´t explainmyself I just get my head back to the book!!!

I suppose I´m not sure why the parameters have to be declared in a seperate variable within the function ,Couldn´t the function be simplified ?

const hof = (n1, n2) => {
let value = n1 +n2
console.log( This function returned ${value}
}

hof

Hmm, so this is the expected answer? Then I would agree that this is not the most generic function possible. Maybe it is just to demonstrate that you can also pass functions as arguments, not only primitives – like numbers or strings.
I think a more generic way would be putting it like this:

let cb = (n1, n2) => n1 + n2;
let hof = (fn, n1, n2) => {
	let value = fn(n1, n2);
    return `this function returned ${value}`
}
console.log(hof(cb, 3, 5));

Do you have a link to the lesson?

https://www.codecademy.com/courses/introduction-to-javascript/quizzes/higher-order-functions-and-iterators

Hi Mirja ! I´m not sure this link will work. I´m very new on Codecademy and this is my second post.

20000 Thank you´s for reading this. I understand that it is just to demostrate the structure variations.

Have a great evening :slight_smile:
Dan

1 Like

Hi Dan,
yes, it works fine. In the preceding lesson about higher order function it says:

  • Functions can be passed into other functions as parameters.
  • A higher-order function is a function that either accepts functions as parameters, returns a function, or both.

So, yes, I think the question is just to solidify the syntax of higher order functions. Assigning fixed values to a function that is passed in as a parameter wouldn’t be seen in practice too often, I think.