1

I am new to Javascript, and sturdying about the concept of higher order function, and using function as parameters.

I got two examples of code from online, and I do not understand what is going to happen.

First one, output will be 0, and I could not print the time of t1 and t2 ( I tried console.log(t1), but it became reference error). I wonder why it became 0. Also, I don't get how funcParameter() that is inside of functionbody is working for the function, though I know funcparameter() is callback function, and which is addOneToOne().

Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()? If I have not put anything in parameter, num will be automatically 0 or undefined?

I am sorry for many questions, but I really appreciate if you could help me out.

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

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

console.log(timeFuncRuntime(addOneToOne))//0


//2nd code

  const addTwo = num => num + 2;


  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,1)

  console.log(checkConsistentOutput(addTwo,1))//3

0

3 Answers 3

1

1: The scope of the t1 variable is limited to the function block. You can read more about variables scope here

2: You are using val as function parameter, and the value of val is 1.

1 + 2 = 3

The 1 is in this line:

checkConsistentOutput(addTwo,1)
Sign up to request clarification or add additional context in comments.

1 Comment

I totally missed about scope! Thank you so much Cristian!
1

First one, output will be 0, and I could not print the time of t1 and t2 ( I tried console.log(t1), but it became reference error). I wonder why it became 0.

Because zero milliseconds passed between the first call to the callback and the second. Date.now() works in milliseconds. The call probably took microseconds at most.

You couldn't access t1 or t2 because they're local variables in the timeFuncRuntime function, so you can't access those variables outside the timeFuncRuntime function, only inside it.

Also, I don't get how funcParameter() that is inside of functionbody is working for the function, though I know funcparameter() is callback function, and which is addOneToOne().

Functions are objects in JavaScript. When you do timeFuncRuntime(addOneToOne), you're passing a reference to the function into timeFuncRuntime which it receives as the value of the funcParameter parameter. So when that code does funcParameter(), it's calling addOneToOne.

Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()?

Because checkConsistentOutput did it. When you do console.log(checkConsistentOutput(addTwo,1)), you're passing a reference to addTwo and the number 1 into checkConsistentOutput. It receives them as its func and val parameters. So when it does func(val), it's calling addTwo with the value 1. It gets the result in the firsttry variable, then does the call again and gets the result in secondtry, and since they match, it returns the value of firsttry, which is 3.

3 Comments

Hi, T.J. Your explanation is easier to understand, and I understand the problem, thank you so much! just one thing, can you explain me why output will be 0 in first code? I tried console.log with t1 and t2 inside of the function and found out they have different number. Then why it ends up as 0? Thanks again for your help.
@NagisaAndo - If t1 and t2 had different values, it wouldn't be 0. So they must have the same value. (Which makes sense, because that call will take almost no time to run at all.)
Yes indeed! Thank you for your help again!
0

Second one, output will be 3. but how can it be 3 even though, I haven't put parameter into addTwo()?

You copy the function stored in addTwo as the first argument to checkConsistentOutput where it gets assigned to func. Then you call the function here — let firsttry = func(val); — and here — let secondtry = func(val);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.