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

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

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