简体   繁体   English

写一个高阶function,checkConsistentOutput()

[英]Write a higher-order function, checkConsistentOutput()

This function should have two parameters: a function and a value.这个 function 应该有两个参数:一个 function 和一个值。 It should call the argument function with the value two times.它应该使用该值两次调用参数 function。 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'如果回调 function 两次产生相同的结果,则应返回 function 调用的结果,否则应返回字符串 'This ZC1C425268E68385D1AB5074C17A94F14 返回不一致的结果'

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.我收到错误 SyntaxError: Missing initializer in const 声明。 please help me understand.请帮我理解。

I'm seeing an error in the last const declaration that appears to be because it's missing an '='.我在最后一个 const 声明中看到一个错误,这似乎是因为它缺少一个“=”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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