Esta función recorre las matrices y en cada objeto, suma el primer número y resta el segundo número
p.ej. numero([[10,0],[3,5],[5,8]]) = (10-0) + (3-5) + (5-8) El total debe ser igual a 5
Problema: estoy usando un ciclo forEach pero devuelve indefinido, cuando hago console.log, el número muestra 5.
var number = function(busStops){ var total = 0; busStops.forEach(function(n){ total = total + n[0] - n[1]; return total; }) }Podrías reducir la matriz.
const number = busStops => busStops.reduce((t, [a, b]) => t + a - b, 0); console.log(number([[10, 0], [3, 5], [5, 8]]));Acabo de encontrar el problema, el retorno debe estar fuera del bucle forEach
var number = function(busStops){ var total = 0; busStops.forEach(function(n){ total = total + n[0] - n[1]; }) return total; }