-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Вычислить факториал | ||
|
||
[Факториал](https://ru.wikipedia.org/wiki/Факториал) натурального числа - это число, умноженное на `"себя минус один"`, затем на `"себя минус два"`, и так далее до `1`. Факториал `n` обозначается как `n!` | ||
|
||
Определение факториала можно записать как: | ||
|
||
```js | ||
n! = n * (n - 1) * (n - 2) * ...*1 | ||
``` | ||
|
||
Примеры значений для разных `n`: | ||
|
||
```js | ||
0! = 1 | ||
1! = 1 | ||
2! = 2 * 1 = 2 | ||
3! = 3 * 2 * 1 = 6 | ||
4! = 4 * 3 * 2 * 1 = 24 | ||
5! = 5 * 4 * 3 * 2 * 1 = 120 | ||
``` | ||
Обратите внимание, что факториал 0 и 1 равен 1. | ||
Это важно и нужно учесть в решении. | ||
|
||
|
||
Задача – написать функцию `factorial(n)`, которая возвращает `n!`, используя цикл (while или for). | ||
Решить нужно именно циклом, а не рекурсией. | ||
|
||
```js | ||
factorial(0); // 1 | ||
factorial(1); // 1 | ||
factorial(3); // 6 | ||
factorial(5); // 120 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function factorial(n) { | ||
// ваш код... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
describe('1-module-1-task', () => { | ||
it('факториал 0 равен 1', () => { | ||
expect(factorial(0)).toEqual(1); | ||
}); | ||
|
||
it('факториал 1 равен 1', () => { | ||
expect(factorial(1)).toEqual(1); | ||
}); | ||
|
||
it('факториал 3 равен 6 ', () => { | ||
expect(factorial(3)).toEqual(6); | ||
}); | ||
|
||
it('факториал 5 равен 120 ', () => { | ||
expect(factorial(5)).toEqual(120); | ||
}); | ||
}); |