forked from aishraj/JavaScript-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001.js
More file actions
45 lines (36 loc) · 837 Bytes
/
001.js
File metadata and controls
45 lines (36 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* <!--
* This program is distributed under
* the terms of the MIT license.
* Please see the LICENSE file for details.
* -->
*/
/*
* Write a function that calculates the factorial of a given number recursively.
*/
/*____________________________________________________________________________*/
/**
* @function {public static} factorial
*
* Recursively calculates the factorial of a given number.
*
* @param {Integer} n - the number to calculate.
*
* @return the factorial of the given number.
*/
function factorial(n) {
if (n > 1) {
return n * factorial(n-1);
}
return 1;
}
/*____________________________________________________________________________*/
console.log(factorial(4));
console.log(factorial(5));
console.log(factorial(6));
/*
Output: ($ /usr/bin/node 001.js)
24
120
720
*/