13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JavaScript] 再帰呼出しで階乗(Factorial)を求めてみよう

Last updated at Posted at 2014-06-17

再帰呼出しを学ぶときによく作るやつ.

サンプル

階乗を求めるコードです.
数学でいう n! ってやつです.

// 階乗
var factorial = function(num) {
    return (num <= 0) ? 1 : (num * factorial(num-1));
};

console.log( factorial(0) );    // 1
console.log( factorial(5) );    // 120
console.log( factorial(6) );    // 720
console.log( factorial(7) );    // 5040

再帰大好き!\(^o^)/
三項演算子も大好き!!\(^o^)/

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?