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

108
Visualizações
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 Respostas
Responde à pergunta

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