challenge-algorithms-v2.0/06-sum/index.js

28 lines
642 B
JavaScript

export function sum(arr=[]){
let soma = 0
if (arr.length == 0){
soma = 0
}
else if (arr.length == 1 && arr[0] ==1){
soma = 1
}
else{
for (let i = 0; i < arr.length; i++){
soma += arr[i]
}
}
return soma
}
console.log(sum([]));//0
console.log(sum([1]));//1
console.log(sum([1, 2]));//3
console.log(sum([1, 2, 3]));//6
console.log(sum([1, 2, 3, 4]));
console.log(sum([1, 2, 3, 4, 5]));
console.log(sum([4, 6, 12, 5]));
console.log(sum([9, 234, 312, 999, 21 , 2311]));
console.log(sum([533, 234, 23423, 32, 48876]));
console.log(sum([-1, 5, -10, -45]));
console.log(sum([-2, -7, -31]));