EN
JavaScript - subtract array of numbers
0
points
In this article, we would like to show you how to subtract array of numbers using JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
const numbers = [10, 1, 2];
const sum = (accumulator, number) => accumulator - number;
console.log(numbers.reduce(sum)); // 7 (10 - 1 - 2 = 7)
Alternative solution
In this section, we present an alternative solution for older JavaScript versions.
// ONLINE-RUNNER:browser;
function subtract(accumulator, number) {
return accumulator - number;
}
var numbers = [10, 1, 2];
var result = numbers.reduce(subtract);
console.log(result); // 7 (10 - 1 - 2 = 7)