Spocoman

10. Factorial Division

Jan 28th, 2022 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (firstNum, secondNum) {
  2.  
  3.     function calculateFactorial() {
  4.         let num1 = 1;
  5.         let num2 = 1;
  6.         for (let i = firstNum; i > 0; i--) {
  7.             num1 *= i;
  8.         }
  9.         for (let i = secondNum; i > 0; i--) {
  10.             num2 *= i;
  11.         }
  12.         return `${(num1 / num2).toFixed(2)}`;
  13.     }
  14.     console.log(calculateFactorial());
  15. }
  16.  
  17. Решение с рекурсия:
  18.  
  19. function solve (firstNum, secondNum) {
  20.  
  21.     function factorial(num) {
  22.         if (num === 0) {
  23.             return 1;
  24.         } else {
  25.             return (num * factorial(num - 1));
  26.         }
  27.     }
  28.     console.log(`${(factorial(firstNum) / factorial(secondNum)).toFixed(2)}`);
  29. }
  30.  
  31. Или:
  32.  
  33. function solve (firstNum, secondNum) {
  34.    
  35.     function factorial(num) {
  36.         return (num < 2) ? 1 : factorial(num - 1) * num;
  37.     }
  38.     console.log(`${(factorial(firstNum) / factorial(secondNum)).toFixed(2)}`);
  39. }
  40.  
Add Comment
Please, Sign In to add comment