Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. function factorial(num)
  2. {
  3. // If the number is less than 0, reject it.
  4.  
  5. if (num < 0) {
  6. console.log('use a number above 1');
  7. return -1;
  8.  
  9. }
  10. // If the number is 0, its factorial is 1.
  11. else if (num == 0) {
  12. return 1;
  13. }
  14. let tmp = num;
  15. while (num-- > 2) {
  16. tmp *= num;
  17. }
  18. return tmp;
  19. }
  20.  
  21. let result = factorial(18);
  22. console.log(result);
  23.  
  24. // Output: 6402373705728000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement