Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda