Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda