I have a problem subtracting the values of an array from one another. The sum works perfectly, but the subtraction does not work. It always makes me negative numbers, and that would not be the case.
This is my code:
var numbers = [];
var userTimes = prompt('Cuantas operaciones quieres hacer?')
for (var i=0; i<userTimes; i++) {
var user = parseInt(prompt('Escribe un valor'));
numbers.push(user);
console.log(numbers);
}
function sum() {
var acc = 0;
numbers.forEach(function(obj){
acc += obj;
})
return acc;
}
console.log('La suma entre ' + numbers + ' hace: ' + sum());
function subs() {
var acc = 0;
numbers.forEach(function(obj){
acc -= obj;
})
return acc;
}
console.log('La resta entre ' + numbers + ' hace: ' + subs());
//Output sum: La suma entre 8,5,1 hace: 14
//Output subs: La resta entre 8,5,1 hace: -14