In line 1 of the program, we inform the browser that our document is written in the HTML language (more specifically, this is the header characteristic of HTML 5). Line 2 contains the <html> tag, which starts the HTML code. Line 3 begins the <body> section of our document, containing the elements that should appear on the page. In our example, the <body> section contains only a script written in JavaScript that implements the iterative factorial calculation algorithm. The start of the script is indicated by the <script> tag in line 4—we can treat this as the START block (1) of our algorithm. The content of the script is contained in lines 5 through 9.
In line 5, we declare the variable n using the keyword var; simultaneously, the assignment operator ("=") initializes this variable with a value entered by the user using the prompt() instruction, which is block (2) of the algorithm. prompt—a standard JavaScript function—displays a pop-up text input window on the screen. The first argument of the function specifies the text displayed as the prompt, and the second is the default value entered into the edit field. In our case, we display the text "n:" as the prompt and don't suggest any default text to the user (the second argument is an empty string ""). The prompt() function returns the text entered by the user into the edit field and then confirmed with the OK button.
In lines 6 and 7, we declare the variables i and s, initializing them simultaneously with the initial value (equal to 1 in both cases), which is block (3) of the discussed algorithm.
The while statement in line 8 is the equivalent of the selection block (4) from our algorithm. The instruction located on the same line after the condition concisely expresses block (5): s∗=i++;. This notation means that the variable s will be assigned its old value multiplied by the value of variable i, and then the value of variable i will be incremented by 1. These instructions will be executed as long as the condition in the while statement is met, meaning as long as i is less than or equal to n. When the constantly incremented i becomes greater than n, we proceed to the instructions after the while loop, starting in line 9, where the result is printed (6).
The </script> tag in line 10 ends the program's execution (the algorithm's STOP block (9)).
Finally, in lines 11 and 12, we close the </body> and </html> sections.
