I’m having troubles knowing the difference between these two recursive functions:
This will output the corrected answer:
const sum = (args) => {
var total = 0
if (args.length === 0){
return 0
}
else {
return args[0] + sum(args.slice(1));
}
}
console.log(sum([1, 2, 3])); // 6
And this will not:
const sum = (...args) => {
var total = 0
if (args.length === 0){
return 0
}
else {
return args[0] + sum(args.slice(1));
}
}
console.log(sum(1, 2, 3)); // 6
Shouldn’t the Rest … turn the args value into an array? Or am I interpreting that wrong?