你可以重写你的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
我认为你应该添加更多关于什么是预期输出(和实际输出)的细节,即使它*有点清楚你要问什么。 –