FAQ: Higher-Order Functions - Functions as Parameters

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

68 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.

Date - JavaScript | MDN

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.

7 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.

I think perhaps they are wanting us to fallback to the documentation or search things we don’t fully understand ourselves. The way I understand it is google and the ability to search and find solutions to our questions is a very important tool in a coder’s arsenal. This forum is one such place to find answers as well as MDN. I have also found answers to questions on Stack andW3.
just to name a few. I have yet to have a question that hasn’t already been asked and answered at some point on some coding site. If coding is what we plan on doing we should definitely become comfortable with searching for answers, I’m sure the people that answer our questions here have questions of their own they have to search for answers to for every day.

3 Likes

I can only speak for myself, but that is a very true statement.

1 Like

Silly question but…

I’ve answered 3.Functions as parameters correctly (based on the assumption that the console on the right is not giving me error messages). But why is my solution not returning/displaying anything in the console? Here’s my code:-

const checkConsistentOutput = (func, val) => {
let firstCall = func(val);
let secondCall = func(val);
if (firstCall === secondCall){
return firstCall;
} else {
return ‘This function returned inconsistent results’;
}
};

checkConsistentOutput(addTwo, 3);

//end code

Many thanks!!