FAQ: Higher-Order Functions - Functions as Parameters

This community-built FAQ covers the “Functions as Parameters” exercise from the lesson “Higher-Order Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Functions as Parameters

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

2 posts were split to a new topic: Better understanding higher order functions - helpful resources

7 posts were split to a new topic: Crossing wires somewhere with callbacks and HOF’s

2 posts were split to a new topic: Higher order functions

4 posts were split to a new topic: Higher order functions as parameters terminology

4 posts were split to a new topic: Do I need to use addOneToOne as the parameter?

A post was merged into an existing topic: Do I need to use addOneToOne as the parameter?

A post was merged into an existing topic: Higher order functions as parameters terminology

2 posts were split to a new topic: Javascript Higher Order Functions as Parameters Autochecker

2 posts were split to a new topic: Mistake in setting values for Functions as Parameters

2 posts were merged into an existing topic: Higher order functions as parameters terminology

A post was merged into an existing topic: Better understanding higher order functions - helpful resources

2 posts were split to a new topic: Functions as parameters: Understanding multiple function evaluation

2 posts were merged into an existing topic: Higher order functions as parameters terminology

@mtf can I say that “functions as parameters” is the same concept like "helper function" + loop ? I remember that example you gave me, about helper function, here’s updated nonsense version of it, with for loop. Is it considered to be “functions as parameters” now? thanks

const product = (a, b) => a * b;

const sideB = () => {

     for (i=0; i<5;i++)

     console.log(product(1023, 3) / 5*i);

}

sideB();

//0

//613.8

//1227.6

//1841.3999999999999

//2455.2

A function as a parameter means that a function is being passed in by reference to another function.

f = x => x;               // identity function

g = (x, f) => x + f(x)    // higher order function

In the definition of g we see f in the parameter list. It could be any symbol, but we know that in this instance we’re passing a function called f. The function we give it to doesn’t care what we call it, since it knows where the real one is by the argument it was given.

 > g(5, f)
<- 10
 > 
1 Like

We’re not out of the water yet, but you see where this is going… If you let your imagination loose.

This exercise is very poorly executed and poorly explained to the audience. It should be broken into numerous exercises, or explained more thoroughly. Begin with the example and break down every line of code like every other example prior to this exercise.

Why am I declaring two new variables inside of a function?
Where does Date.now() come from?
If it is a method then why isn’t there a link to MDN like every other exercise that references a new method???
*Is funcParameter a parameter or a new function?
If it is a parameter, like I assume, then why does the first line of the explanation given state: “It takes in a function as an argument…” ?
I can only assume that the author has decided to escape the logic of chronological order and reference this portion of the code (which is the last line of the code):
timeFuncRuntime(addOneToOne);

This whole exercise is mind boggling. We (the audience) are given a whole narrative about baking a cake in the first slide. Told about taking directions for granted, yet we are then thrown into an exercise that just assumes about 15 things.

FuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
}
const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

5 Likes

We need each of them to be unique so we can then compute their difference.

Date is special object with several methods for computing dates and times.

Date.now()

.now() is a method of the Date object that when invoked returns the system time and date in the form of a timestamp. How it does this is of no concern, but what the return value looks like is something we should know…

Date.now()
1568400620152
Date.now()
1568400633459

A timestamp can be normalized to resolve year, month, day, hour, minute, second and milliseconds.

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC. This date and time is called the Unix epoch , which is the predominant base value for computer-recorded date and time values.

Slight oversight, perhaps.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

The parameter is a reference to another function, namely the one we pass in as the argument. It can be any function.

Above the argument of the function call is a reference to function addOneToOne().


1568400633459/1000
1568400633.459
1568400633/3600
435666.8425
435666/24
18152.75
18152/365
49.73150684931507

We can see that 1970 was 49 years ago. The above is not exactly how the normalization takes place, but it is close enough to serve as an illustration.

2 Likes

I’m facing the exact same problem as you. The only difference being you seem to understand it more. Could you perhaps explain it to me? Thank you.

1 Like