Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!