Skip to content

Factorial (loop)

JavaScript for with accumulation

Same task in: C++

Flowchart (ISO 5807)

StartInput nr = 1i = 2, n, 1r = riReturn rEndFigure 1 — factorial

Source code

JavaScript
function factorial(n) {
    let r = 1;
    for (let i = 2; i <= n; i++) {
        r *= i;
    }
    return r;
}

The editor opens with this code — change it and the chart redraws.

Language
JavaScript
Constructs
for with accumulation
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples