Fireflies

As dusk darkens my yard, hundreds of tiny lights rise from the grass, like silent fireworks. The yard is alive with light. As I watch these little stars twinkle, it is hard to remember that this is…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How Recursion works in JavaScript

This is what DALL-E gave me when I typed in Recursion and I’m sticking with it because it’s terribly amazing!

The factorial of a non-negative number is the product of all positive numbers less than or equal to that number. For example, the factorial of 5 (written as 5!) is calculated as 5 * 4 * 3 * 2 * 1, which equals 120.

In other words, the factorial of the number 5 is 120.

The most important thing to understand about recursion is that you almost always need an explicit base case. This is the “until it doesn’t” part of the definition MPJ gives about recursion. Basically you need to tell it when to stop calling itself because it will max out on the call stack, which is kinda (but not really) like an infinite loop.

Note: You can still get a max call stack error even if you have a base case because the computation your giving it is too large to fit in the call stack.

In the example below the explicit base case is the conditional of when n is equal to 0, or when n is equal to 1.

Add a comment

Related posts:

What can I vote for?

Combining both economic and utility functions, DRATE token enables DeFi Rating users to participate in platform governance and decide on its future. Striving to provide a fair and mature governance…

Food Product Development

What Is the Development of Food Products? The difficult procedure required to release a new or enhanced product onto the market is called product development. This is done in the food sector by…

Oprah Exposed for Wearing Crumpled Boots

One of the most insidious characteristics of fake news and misinformation is the use of leading questions to infer a plausible but nonexistent fantasy. Sometimes, the purveyor attempts to rationalize…