• + 0 comments

    Using for loop:

    const factorial = (n) => {
        let fact = 1;
        for( let i = n; i>0; i--){
            fact = fact*i
        }
        return fact;
    }
    

    Using recursion:

    const factorial = (n) => {
        if (n == 1) return 1;
        return n * factorial(n-1);
    }
    

    OR

    const factorial = (n) => n == 1 ? 1 : n * factorial(n-1);