let arr = [2.1, 3.1, 4.1, 6.1, 7.1, 9.1, 11.1, 0.0];
function sum(arr) {
let s = 0;
for (let i = (k = arr.lastindexOf(0)) > 0; arr.length - 1; i--) {
s += Math.abs(arr[i]);
}
return s;
}
console.log(sum(arr));
function sum(arr) {
let result = 0;
for (let i = (arr.length - 1); i !== 0; i--) {
if (arr[i] === 0) return result;
result += Math.abs(arr[i]);
}
return result;
}
console.log(sum([2.1, 3.1, 4.1, 6.1, 7.1, 9.1, 11.1, 0.0, 2.2, 1.4])); // 3.6
console.log(sum([2.1, 3.1, 4.1, 6.1, 7.1, 9.1, 11.1, 0.0])); // 0
function sum(numbers) {
const index = numbers.findLastIndex(n => n === 0);
if (index < 0) {
return 0;
}
return numbers
.slice(index)
.reduce((acc, num) => acc + num, 0);
}
console.log(sum([2.1, 3.1, 4.1, 6.1, 7.1, 9.1, 11.1, 0.0])); // 0
console.log(sum([2.1, 3.1, 4.1, 6.1, 7.1, 9.1, 11.1, 0.0, 3.1, 4.1])); // 7.199999999999999