Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Correcting the loop
Something went wrong, and instead of printing pages one through seven, the driver prints a blank page and then pages one through six. Let’s look at the sequence in which the parts of the for
structure are implemented using this example:
for (var i = 0; i < 5; i = i + 1) {
console.log(i);
}
- First,
var i = 0;
works.i
variable will be created, which is equal to0
. - Then the
i < 5;
test will be performed. Sincei
is now equal to0
and this value is less than5
, the condition returnstrue
. This means that the loop can continue working and go to execution of the code from the body of the loop. - The code will be executed from the body of the loop. In our case, the console will log
0
, the current value of thei
variable. - Then
i = i + 1
will be executed. The variablei
becomes equal to1
. - Then
i < 5;
will be tested. It will returntrue
once again, since1
is less than5
. - The body of the loop is executed again, then the value
i
will increase by one, and so on in a loop until the conditioni < 5;
doesn’t returnfalse
. Then the loop will finish its work.
Now let’s look at the example from the previous task. Why was the blank page printed first?
On the first go-round or, scientifically speaking, during the first iteration of the loop, variable page
still equals zero. And it increases to one already after the first iteration. If you change the initial value of the variable page
from 0
to 1
, then the problem disappears.
Why are there six pages, and not seven? Let’s take a look at the loop step-by-step:
Preparation: totalPages = 7; page = 0
1 iteration: page = 0; 0 < 7? yes! Printing page 0; page = 1
2 iteration: page = 1; 1 < 7? yes! Printing page 1; page = 2
3 iteration: page = 2; 2 < 7? yes! Printing page 2; page = 3
4 iteration: page = 3; 3 < 7? yes! Printing page 3; page = 4
5 iteration: page = 4; 4 < 7? yes! Printing page 4; page = 5
6 iteration: page = 5; 5 < 7? yes! Printing page 5; page = 6
7 iteration: page = 6; 6 < 7? yes! Printing page 6; page = 7
8 iteration: page = 7; 7 < 7? no! Finish the loop!
To get the seventh page, you need to change the comparison from “less than” to “less than or equals to”. In this case, the check on the eighth iteration of the loop will work and the seventh page will be printed.
8 iteration: page = 7; 7 <= 7? yes! Printing page 7; page = 8 9 iteration: page = 8; 8 <= 7? yes! Finish the loop!
To summarize: to make everything print as it should, we need to start counting from page number 1
and use the <=
sign in condition to include the last page in the loop.
- script.js
Thanks! We’ll fix everything at once!
The code has changed, click “Run” or turn autorun on.
Result
Let’s correct the print mode for all pages.
- Replace the initial value of the variable
page
to1
. - Replace “less than” with “less than or equals to” in the loop condition.
- To make sure that everything works, set the variable
totalPages
to value5
. You should get five pages, starting with the first.
Comments