Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!