2022-11-01 00:15:32 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 15:07:13 +00:00
|
|
|
|
2022-11-01 00:15:32 +00:00
|
|
|
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]));
|