Higher-Order Function - Question on the given examples

Hi
I just have some question on the given example in the course.

I was just wondering, what does the following function do exactly??

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

This part I get (as mentioned in comment)

const timeFuncRuntime = funcParameter => {
  let t1 = Date.now(); //date now
  funcParameter(); //checkThatTwoPlusTwoEqualsFourAMillionTimes() run it  (but what does this do???)
  let t2 = Date.now(); //date now
  return t2 - t1;//calculate date 1 minus date 2 and retruns it
};

const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

console.log(time2p2); // gets the value of the above higher order function call...

Thank you

its just a function which will take some time to complete, so we can measure execution time.

The function name is pretty descriptive:

checkThatTwoPlusTwoEqualsFourAMillionTimes

we have a loop which checks a million times that 2 + 2 equals 4

How does it check if 2+2 = 4 thought? Just wondering…
Since I don’t see any parameters or equation that shows 2+2 = 4 in here
Except that IF 2+2 doesn’t = 4,then we get a log message…?

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

Thank you

so if 2 + 2 equals 4, nothing happens. Otherwise you get a message

Ahh, so is it because the condition

if ( (2 + 2) != 4) 

only states if 2+2!=4, therefore as long as this condition is not met; nothing happens?

Thank you so much for the assistance.

2 Likes