Escriba una función llamada sumArray
que acepte un solo argumento: una matriz de números. Debería devolver la suma de todos los números en la matriz.
Condición:
Necesitará una variable para realizar un seguimiento del total. Debe comenzar como cero.
Recorra la matriz para cada elemento, agréguelo a la variable total.
function sumArray(n1, n2, n3) { let total = n1 + n2 + n3; return total; } sumArray([3,45,66]);
Simplemente haga un bucle desde la matriz y obtenga la salida
function sumArray(arr){ let total = 0; arr.forEach((i) => total+=i); return total; } console.log(sumArray([3,45,66]));
O puedes probar con reduce como explica Cristian
function sumArray(arr){ const reducer = (previousValue, currentValue) => previousValue + currentValue; let total = arr.reduce(reducer) return total; } console.log(sumArray([3,45,66]));