你可以重寫你的maxOfThree到這一點:如果你需要使用文件撰寫
function maxOfThree(a, b, c) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FMath%2Fmax
return Math.max(a, b, c);
}
我會這樣做:
function maxOfThree(a, b, c) {
return Math.max(a, b, c);
}
document.write(maxOfThree(1,2,3));
但我會建議你,如果你需要出示你的數據使用console.log
用於調試和寫入DOM節點:
var n_max = maxOfThree(1,2,3);
var d_text = document.createTextNode(n_max);
document.body.appendChild(d_text);
當你調試:
var n_max = maxOfThree(1,2,3);
console.log(n_max);
如果你想使一個功能maxOfN
它可以這樣做:
function maxOfN() {
// Call the Math.max function with all the arguments that maxOfN gets and also set the correct context (Math).
return Math.max.apply(Math, arguments);
}
console.log(maxOfN(1)) // 1
console.log(maxOfN(1,2,3,4,5)) // 5
您的函數在'document.write'語句之前返回。 –
就這麼你知道,你可以用'Math.max'來代替你的手工功能。參考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FMath%2Fmax – Markasoftware
我認爲你應該添加更多關於什麼是預期輸出(和實際輸出)的細節,即使它*有點清楚你要問什麼。 –