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

149
Views
Make a recursive function in JavaScript

I am trying to make a recursive function for this parameters. Function should determine the nth element of the row

a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...

I am trying really hard to do this but i cant get a result. Please help me to do this

I came up with this but it is not a recursive function. I can not do this as a recursive function

let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
    a = i*a + 1/i
}
console.log(a)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's the recursive function you're looking for, it has two conditions:

  • k == 0 => 1
  • k != 0 => k * fn(k - 1) + 1/k

function fn(k) {
  if(k <= 0) return 1;
  return k * fn(k - 1) + 1/k;
}

console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));

Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k

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!