这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion snippets/factorial.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
### Factorial

Create an array of length `n+1`, use `reduce()` to get the product of every value in the given range, utilizing the index of each element.
Use recursion. If `n` is less than (for safety) or equal to `1`, return `1`. Otherwise, return the product of `n` and the factorial of `n - 1`.

```js
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1)
```

Another way: create an array of length `n+1`, use `reduce()` to get the product of every value in the given range, utilizing the index of each element.

```js
var factorial = n =>
Expand Down