• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

299
Vistas
Can I pass different parameter instead of (n1,n2,n3)?

Write a function called sumArray which accepts a single argument: an array of numbers. It should return the sum of all the numbers in the array.

Condition:

  1. You will need a variable to keep track of total. It should start out as zero.

  2. Loop over the array for each element, add it to total variable.

function sumArray(n1, n2, n3) {
  let total = n1 + n2 + n3;
  return total;
}

sumArray([3,45,66]);

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just loop from array and get the output

function sumArray(arr){
  let total = 0;
  arr.forEach((i) => total+=i);
  return total;
}
console.log(sumArray([3,45,66]));

Or you can try with reduce as explained by Cristian

function sumArray(arr){
  const reducer = (previousValue, currentValue) => previousValue + currentValue;
  let total = arr.reduce(reducer)
  return total;
}
console.log(sumArray([3,45,66]));

about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda