繁体   English   中英

写一个高阶function,checkConsistentOutput()

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

这个 function 应该有两个参数:一个 function 和一个值。 它应该使用该值两次调用参数 function。 如果回调 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'
  }
}

我收到错误 SyntaxError: Missing initializer in const 声明。 请帮我理解。

我在最后一个 const 声明中看到一个错误,这似乎是因为它缺少一个“=”。

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

暂无
暂无

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

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