Sir I don't know how to ask question in the forum But I want know about this function that how it's working!


#1

Sir I want to know how this programme is working. I am a beginner in this course so if you let me understand this, then it’ll be really helpful for me.

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
const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

const checkConsistentOutput = (timeFuncRuntime, val) => {
  let try1 = timeFuncRuntime(val);
  let try2 = timeFuncRuntime(val);
  if (try1 === try2) {
    return try1;
  } else {
    return 'This function returned inconsistent results'
  }
}
console.log(checkConsistentOutput(addTwo, 7));