Javascript calculator using rest parameter

I have been trying to solve this problem wherein any number of arguments can be passed and the function has to calculate the sum of all the values passed. Can you please help me understand what mistake I am making?

//complete the function

function Add(…nums){

let sum = 0;

for (let num of nums){

    return sum += num;

}

}

console.log(Add(1,2,3));

console.log(Add(4,14,5,9,14));

console.log(Add(2,36));

return sum+=num; function will return on executing this statement without iterating through rest of the arguments in for loop.

return sum+= num; will exit for loop and exit function.

1 Like

I got it. I should have returned outside the for loop. Thanks buddy!!!

1 Like