Advertisement
vit134

Вычисление факториала

Nov 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Вычисление факторила
  3.     n! = 6! = 1 * 2 * 3 * 4 * 5 * 6
  4. */
  5.  
  6. function factorial(num) {
  7.  
  8.     if (num === 0) return 0;
  9.     if (num === 1) return 1;
  10.    
  11.     let last = 1;
  12.    
  13.     for (var i = 1; i < num; i++) {
  14.         last = last * (i + 1)
  15.     }
  16.    
  17.     return last;
  18. }
  19.  
  20.  
  21. console.log(factorial(0)) // 0
  22. console.log(factorial(1)) // 1
  23. console.log(factorial(6)) // 720
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement