Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this an example of recursion in JavaScript?

I'm currently a bit befuddled... I'm tasked with finding the value of x, is this an example of recursion? I am not asking for a straight answer for the value of x, I can find that out with some guidance from the Overflow community. I get confused at a = b, and b = t, then where is says return the function.

Any and all help is greatly appreciated!

function f(a, b, c) {
  if (c > 1) {
    c = c - 1;
    var t = a + b;
    a = b;
    b = t;
    return f(a, b, c);
  }
  return a + b;
}
var x = f(1, 3, 4);
like image 452
Brittany LaCoste Avatar asked Apr 17 '26 04:04

Brittany LaCoste


2 Answers

Yes, this line here: return f(a, b, c) - this is recursion as it is calling itself at the end. It's modifying the parameters that it receives and re-calling itself again.

like image 124
Christopher Messer Avatar answered Apr 19 '26 16:04

Christopher Messer


Yes, this is an example of recursion. You can tell this because function f() calls function f() (itself) with a smaller subset on line 7.

You might find this documentation from Microsoft helpful.

like image 21
Jacobm001 Avatar answered Apr 19 '26 17:04

Jacobm001



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!