簡體   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