JavaScript Code

Factorial of a Number using For Loop

Problem Statement

given a number, write a JavaScript program with a function that takes a number as argument, find the factorial of the number using for loop, and returns the result


initialise factorial with 1

inside for loop, iterate variable i from 1 to n, and multiply assign factorial with i

after the loop is executed, factorial contains the factorial of n


1. factorial of a number using for loop

function findFactorial(n) {
    var factorial = 1;
    for (var i = 1; i <= n; i++) {
        factorial *= i;
    return factorial;

var n = 5;
console.log(n+'! = '+ findFactorial(n));