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

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

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