首页 > 解决方案 > 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);

标签: javascript

解决方案


你的代码工作得很好。您实际上并没有在任何地方输出结果:

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}`);


推荐阅读