+ 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

16th Feb 2022, 1:15 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
3 Respostas
+ 3
Morpheus ! I’ve been waiting to see you here. Thank you for that advice, and I will try it! 🙏
16th Feb 2022, 2:12 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 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));
6th Nov 2022, 9:03 PM
Khouloud Mekni
Khouloud Mekni - avatar
+ 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. 👍
16th Feb 2022, 1:48 AM
Morpheus
Morpheus - avatar