🥣 Cumulative Sum
Solving basic algorithms with plain JavaScript
I am an odd number. Take away one letter and I become even. What number am I?
Cumulative Sum Interview Question
Create a function that takes an array of numbers and returns a number that is the sum of all values in the array.
Cumulative Sum Implementation
// Solution 1
function cumSum(arr) {
return arr.reduce((acc, cur) => acc + cur, 0);
}
// Solution 2
export function cumSum(arr) {
let total = 0;
for(let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}
console.log('sum: ', cumSum([1,3,5,7]));