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

262
Visualizações
Why does my function work without a return statement (JS)?

I'm learning javascript; I'm confused about when to use 'return.' So far I know 'return' is used either to specify exactly what value to return or to stop a function from running.

But, when comparing my solution to the answer key, its function required a return statement, but mine did not. Yet, both displayed the same results on the console. Why? Are the results actually different?

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

My Solution:

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

const gradePercentage = (scores) => {
  const perc = (scores / 50) * 100;
  percentages.push(perc);
};

let i = 0;
while (i < testScore.length) {
  gradePercentage(testScore[i]);
  i++;
}
console.log(percentages);

Answer Key

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

const gradePercentage = (scores) => {
  return (scores / 50) * 100;
};

let i = 0;
while (i < testScore.length) {
  const perc = gradePercentage(testScore[i]);
  percentages.push(perc); 
  i++;
}
console.log(percentages); 

Both displayed this result:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you run your function, you directly mutate percentages by calling percentages.push(perc);, while the answer key's gradePercentage function does not mutate percentages.

The answer key does what is called pure function, a deterministic function which has no side effects. Personally, I prefer to write this sort of functions, since they can be easily tested.

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