Create factorial using arrow function operator - Javascript Function

Javascript examples for Function:Arrow Function

Description

Create factorial using arrow function operator

Demo Code

// 8./*from  www . j  a v  a  2s  . com*/
let factorial = x => {
    if (IsNaN(x) || x < 0 || x > 100) return;
    if (x % 1 !== 0) {
        x = parseInt(x);
    }
    if (x === 0) return 1;
    if (x === 1) return 1;
    return x * factorial(x - 1);
}

console.log(`Factorial of 5 is ${factorial(5)}`);

Related Tutorials