javascript - ES6 休息参数代码不起作用
问题描述
为什么这段代码不求和?我正在尝试使用 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}`);
推荐阅读
- java - Apache Beam GroupByKey 重复事件
- javascript - 单击按钮时如何生成一组数组,而不会使第一个消失?
- python - 我正在运行的 python 版本之间的混淆
- python - python列表操作来比较元素的一部分而不是完整的元素
- python - 如何从 python 列表中的字符串中删除不需要的 html 代码?
- python - 没有找到任何参数的 Django NoReverseMatch 错误。已尝试 1 种模式
- java - 在空对象引用上获取 NullPointerException int android.media.MediaPlayer.getDuration()'
- image-processing - 与其他类相比,如何将图像数量较少的类的数据加倍?
- python - Asciimatics 键盘输入
- c# - 不完整的堆栈跟踪信息