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

102
Visualizações
Sum of Digits works in console but not when returned

So I'm doing a codewars challenge and I have no clue why my code isn't working. I'm a beginner so please don't hate on me.

This is my code:

function digital_root(n) {
    let str = n.toString()
    let arr = []
    let sum = 0
    for (let i = 0; i < str.length; i++) {
      arr.push(str.charAt(i))
    }
    for (let i = 0; i < str.length; i++) {
      sum += Number(arr[i])
    }
    let sumStr = sum.toString()
    if (sumStr.length > 1) {
      digital_root(sum)
    } else  if (sumStr.length == 1) {
        return sum
    }
}

It works when I console.log it but not when I return the value. I'm trying to learn recursion. Thanks for the help!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to return digital_root(sum) too, if sumStr.length > 1 in order to access recursive returned value.

you have to write return digital_root(sum) instead of just digital_root(sum).

check below:

function digital_root(n) {
    let str = n.toString()
    let arr = []
    let sum = 0
    for (let i = 0; i < str.length; i++) {
      arr.push(str.charAt(i))
    }
    for (let i = 0; i < str.length; i++) {
      sum += Number(arr[i])
    }
    let sumStr = sum.toString()
    if (sumStr.length > 1) {
      return digital_root(sum)
    } else  if (sumStr.length == 1) {
        return sum
    }
}

console.log("Digital Root :", digital_root('123456789'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Ok, it seems you are missing dealing with the return value of digital_root when you call it recursively. See added "return" statement below.

function digital_root(n) {
    let str = n.toString()
    let arr = []
    let sum = 0
    for (let i = 0; i < str.length; i++) {
      arr.push(str.charAt(i))
    }
    for (let i = 0; i < str.length; i++) {
      sum += Number(arr[i])
    }
    let sumStr = sum.toString()
    if (sumStr.length > 1) {
      // ***** You need to deal with the return value of digital_root when you call it. 
      return digital_root(sum)
    } else  if (sumStr.length == 1) {
        return sum
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

However, while JavaScript's functional coding style does support recursive functions, we need to be aware that most JavaScript compilers are not currently optimized to support them safely. Recursion is best applied when you need to call the same function repeatedly with different parameters from within a loop.

Please read this,

https://www.sitepoint.com/recursion-functional-javascript/#:~:text=However%2C%20while%20JavaScript's%20functional%20coding,parameters%20from%20within%20a%20loop.

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