Javascript Control Flow Question

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

const addTwo = num => num + 2;

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

// Write your code below
let time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

const checkConsistentOutput = (func,val) => {
  let first = func(val);
  let second = func(val);

  if(first == second) {
    return first;
  } else {
    return 'This function returned inconsistent results';
  }
}

checkConsistentOutput(addTwo,2);

Can any one explain the flow of this program?

The bulk of the flow is in the loop of the first function which is called from the second function. The loop runs a million iterations then returns to the runtime function. A timestamp is acquired before the call, and after the return. Their difference is the run time.

1 Like