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

95
Vistas
Recursive Factorial inside Object Javascript

const Calculate = {
  factorial(n) {
    return n * factorial(n - 1)
  }
}
let newNumber = Calculate.factorial(8);

So every time I call the function I get a factorial not defined error. I'm guessing it has something to do with the function being inside an object. Need some help in understanding whats going on. Thank you in advance

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

0

Objects don't create a variable scope, so you need to refer to the object when calling the method.

You can also use this to refer to the object that the method was called on, so you don't have to hard-code the variable name.

const Calculate = {
  factorial(n) {
    if (n <= 1) {
      return 1;
    }
    return n * this.factorial(n - 1)
  }
}
let newNumber = Calculate.factorial(8);
console.log(newNumber);

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