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

131
Views
Getting 'undefined' when trying to return a value from a recursive function

So I have this recursive function which takes in two parameters, factor and width. The factor will decrement in every recursive instance by 0.05. And in every instance it will be multiplied with width to get a value.

If this value is greater than 900 then the recursive function will continue. If it is less than 900 then it will stop and return the factor value.

Right now I'm getting undefined but if I log all the factors then I can see that there are numbers before undefined but it stops with undefined.

How can I get the factor value which is just before undefined?

Here's the snippet:

function findFactor(factor, width) {

  let nextFactor = factor - 0.05;
  let value = width * nextFactor;

  console.log(nextFactor);

  if (value < 900) {
    return nextFactor;
  } else {
    findFactor(nextFactor, width);
  }

}

console.log(findFactor(1, 2400));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are properly returning the result in case if (value < 900) {. However, you forgot to return the result of the subsequent recursive call. Adding return should solve your problem:

else {
  return findFactor(nextFactor, width);
}
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!