JavaScript - Return the factorial of the provided integer



Return the factorial of the provided integer. If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n. Factorials are often represented with shorthand notation n!. For example: 5! = 1 * 2 * 3 * 4 * 5 = 120 . Only integers greater than or equal to zero will be supplied to the function. 


Solution 1 Recursion Method


console = { 
    log: print,
    warn: print,
    error: print
};
function factorialize(num) {
    if (num <= 0) return 1;
    else {
        return num * factorialize(num - 1);
    }
}
console.log(factorialize(5));

Output:


120

Solution 2 Iterate Method


console = { 
    log: print,
    warn: print,
    error: print
};
function factorialize(num) {
    for (var product = 1; num > 0; num--) {
        product = product *= num;
    }
    return product;
}
console.log(factorialize(5));

Output:


120

Solution not working or have any suggestions? Please send an email to [email protected]



Join Our Facebook Group


Share this example


Download Android App

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube