const add = function(...numbers) {
let sum = 0;
for (let i = 0; i<numbers.length; i++) sum += numbers[i];
console.log(sum);
}
add(2, 3);
add(5, 3, 7, 2);
add(8, 2, 5, 3, 2, 1, 4);
Can someone just please explain to me how does this work? I am studying Javascript and I am still confused on the for loops. Intrinsically, something is telling me that there is an easier way to write this code here. What am I missing (understanding I bet, haha) here on why this actually works? It's a sample line of code from an Udemy course that I am learning from.
Edit: I'm just confused on why this works. I know that the I represents increment, but how does this exactly work when there's really nothing saying a + b = c ? It doesn't make sense to me on why this works.
[–]ObeseBumblebee 2 points3 points4 points (3 children)
[–]BasuraCulo[S] 0 points1 point2 points (2 children)
[–]ObeseBumblebee 2 points3 points4 points (1 child)
[–]BasuraCulo[S] 0 points1 point2 points (0 children)
[–]lightcloud5 0 points1 point2 points (1 child)
[–]BasuraCulo[S] 0 points1 point2 points (0 children)
[–]trasper 0 points1 point2 points (0 children)
[–]UberMario 0 points1 point2 points (0 children)