+ 2
Rest & Spread: Summary Calculator
You are making a program to calculate the sum of any number of values. Complete the given function so that it takes as parameters as many numbers as needed and returns the sum. // I want to understand this, but no luck after 30 + minutes. Please help. https://code.sololearn.com/cT4Xmj400fpV/?ref=app
3 Respostas
+ 3
Morpheus ! I’ve been waiting to see you here. Thank you for that advice, and I will try it! 🙏
+ 2
it works with me
function Add(...numbers){
let sum = 0
for ( let number of numbers){
sum += number
}
return sum;
}
console.log(Add(1,2,3));
console.log(Add(4,14,5,9,14));
console.log(Add(2,36));
+ 1
In your code, 'rest' variable is already an array.
No need to initialize it again.
We just need to run a for loop over the rest array, add all the items and return the final sum.
That's it. 👍