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

142
Views
Why is my function returning a sparse array (javascript)?

I'm learning Javascript. This is a problem I'm stuck on:

"Using a for loop, calculate the percentage of students' test scores stored in the testScore array. The test has a total of 100. Store the percentages in another array and display it to the console."

This was my solution (I know there's "better" alternative methods of solving this, but since I'm just learning, I'd rather understand first what I'm doing wrong here.):

const testScore = [10, 40, 30, 25];
const percentages = [];

const gradePercentage = (scores) => {
  percentages[scores] = (scores / 100) * 100;
  return percentages;
};

for (let i = 0; i < testScore.length; i++) {
  gradePercentage(testScore[i]);
}

console.log(percentages);

I got the calculations right, but when trying to store it in another array, I get "empty" slots. Why?

This is what the console displayed:

console

about 4 years ago · Juan Pablo Isaza
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!