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

174
Vistas
How to find factorial using a function with constraints in JavaScript

I am trying to find the factorial of an integer using a function with the following constraint

1<=N<=10

Below is my code but when I execute it I am receiving NaN! Can you please explain and show me where is my mistake. Much appreciated!

const factorial = function (n) {
  if (n >= 1 && n <= 10) {
    return n * factorial(n - 1);
  }
};

console.log(factorial(5));

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try to follow your code step by step and you will find the issue quite easily.

When you don't return anything and you tried to access the returned value you get undefined.

Imagine your type factorial(3)

Your code will run 3 * 2 * undefined

You need to return 1 when n = 1.

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