简体   繁体   中英

Write a higher-order function, checkConsistentOutput()

This function should have two parameters: a function and a value. It should call the argument function with the value two times. If the callback function produces the same result twice, it should return the result of the function call, otherwise, it should return the string 'This function returned inconsistent results'

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(func, val) => {
  let checkOne = func(val);
  let checkTwo = func(val);
  if (checkOne === checkTwo){
    return checkOne;
  } else {
    return 'This function returned inconsisent results'
  }
}

I'm getting the error SyntaxError: Missing initializer in const declaration. please help me understand.

I'm seeing an error in the last const declaration that appears to be because it's missing an '='.

const checkConsistentOutput(func, val) => {
   ...
}
const checkConsistentOutput = (func, val) => {
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM