extraLongFactorials.js

    0

    1

    The code creates a function named extraLongFactorials that takes an argument of type number. Inside of this function, the code creates a variable named memoization with an array of two elements. The first element in the memoization array is of type number, while the second element is of type BigInt. The function then defines a function named factorial that takes a number argument and returns a number. Finally, the code calls the extraLongFactorials function with the value of n as an argument.

    When the code calls the extraLongFactorials function, it is first checking to see if the function can be executed because it is defined as a function that takes a number as an argument. If the function can be executed, the code is calling the factorial function with the value of n as an argument. The code is then storing the result of the factorial function in the memoization array. Once the code has stored the result of the factorial function, it is returning the value of the memoization array instead of the factorial function itself.

    function extraLongFactorials(n) {
      let memoization = [BigInt(0), BigInt(1)];
    
      const factorial = num => (typeof memoization[num] !== 'number')
        ? ((num - BigInt(1)) > 0
          ? (num * factorial(num - BigInt(1)))
          : BigInt(1)
        )
        : memoization[num]
    
      console.log(String(factorial(BigInt(n))));
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.