You are given an array of numbers called numbers. Calculate the sum of numbers and return it from the function. If the next number is equal to 0, the program must stop processing the numbers and return the result.
Sample Input 1:**
11 12 15 10 0 100
Sample Output 1:
48
Sample Input 2:
100 0 100
Sample Output 2:
100
I can't get it to work, this is what I've done so far...
Should I create a counter that will sum up every time it goes in the loop (to check if it's a 0) and another one that gets the 'n' value and sums it up the numbers?
function sum(numbers) {
let add = 0;
for (let n in numbers) {
if (numbers[n] === 0) {
break;
}
if (numbers[n] !== 0) {
add = numbers[n] + n;
n++;
}
return (numbers[n]);
}
}