Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
Finding the sum of a "counter" variable loop that ran ten times then was pushed into the "numbers" array. Each way I tried resulted with a list

I'm asking for help to find the sum of an array with elements that were pushed from a counter variable that had previously looped 10 times. I'm new to Javascript and was practicing for an assessment, and I've tried several different ways to do it and have only resulted with just a list of the elements within the numbers array.

var counter = 10;
var numbers = [];

for (i = 1; i <= 10; i ++) {
  counter = [i + 73];
  numbers.push(counter);
}

console.log(numbers);

function sum(arr) {
  var s = 0;
  for(var i = 0; i < arr.length; i++) {
     s = s += arr[i];
  }
  return s;
}

console.log(sum([numbers]));

function getArraySum(a) {
  var total = 0;
  for (var i in a) {
    total += a[i];
  }
  return total;
}

var numbers = getArraySum([numbers]);
console.log(numbers);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you should push only the value of counter without the brackets and then make a reduce to have the sum of each number in the array

var counter = 10;
var numbers = [];

for (i = 1; i <= 10; i++) {
  counter = i + 73;
  numbers.push(counter);
}

console.log(numbers.reduce((a,b) => a+b));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You had a couple of typos in the code:

Typos
  1. You were wrapping the sum in square brackets:
counter = [i + 73];

You should just remove the brackets like:

counter = i + 73;

2. You were wrapping a value that is already an array in square brackets while passing it as an argument to a function:
sum( [numbers] )

// ...

getArraySum( [numbers] );

You should remove the brackets, like this:

sum( numbers );

// ...

getArraySum( numbers );

Fix

I updated the code that you shared to fix the above-mentioned things:

var numbers = [];

// Loop 10 times and push each number to the numbers array
for (var i = 1; i <= 10; i ++) {
  var sumNumbers = i + 73;
  numbers.push(sumNumbers);
}


console.log(numbers);


function sum(arr) {
  var total = 0;

  for(var i = 0; i < arr.length; i++) {
     total += arr[i];
  }

  return total;
}

// Call the function by passing it the variable numbers, holding an array
var result1 = sum(numbers);
console.log( result1 );



function getArraySum(a) {
  var total = 0;
  for (var i in a) {
    total += a[i];
  }
  return total;
}

var result2 = getArraySum(numbers);
console.log(result2);
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda