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

110
Views
what's wrong with below recursive function?

its not returning anything, but return true or false are getting executed fine. Whenever array contains all odd number it should reach return true as array will be empty and return false if array element is not a odd number.

function isOdd(a) {
  return a % 2 == 1 ? true : false
}

function someRecursive(ary, isOdd) {
  if(ary.length == 0){
      return true;
  } 
  if(isOdd(ary[0])){
      someRecursive(ary.slice(1), isOdd);
  }else{
      return false;
  }  
}

someRecursive([1,3, 5], isOdd);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not returning anything inside if condition. Return the value returned by the recursive function.

Some thing like this :

if(isOdd(ary[0])){
      
      return someRecursive(ary.slice(1), isOdd);
  }

function isOdd(a) {
  return a % 2 == 1 ? true : false
}

function someRecursive(ary, isOdd) {
  if(ary.length == 0){
      return true;
  } 
  if(isOdd(ary[0])){
      
      return someRecursive(ary.slice(1), isOdd);
  }else{
      return false;
  }  
}

console.log(someRecursive([1,3, 5], isOdd))

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!