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

266
Vistas
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 Respuestas
Responde la pregunta

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 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