为什么这段代码没有求和? 我正在尝试使用 javascript ...rest 参数
function sum(...nums) {
let total = 0;
for(const num of nums) {
total += num;
}
return total;
}
sum(10, 36, 7, 84, 90, 110);
最佳答案
你的代码工作得很好。您只是没有实际将结果输出到任何地方:
function sum(...nums) {
let total = 0;
for(const num of nums) {
total += num;
}
return total;
}
let res = sum(10, 36, 7, 84, 90, 110);
console.log(`Total: ${res}`);
关于javascript - ES6 剩余参数代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662686/