HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-03-09
1 <p>The driver can make three copies of the given page. Now you need to train it to make a different number of copies depending on the situation. You know the conditions, and that’s why you could write the program like this:</p>
1 <p>The driver can make three copies of the given page. Now you need to train it to make a different number of copies depending on the situation. You know the conditions, and that’s why you could write the program like this:</p>
2 // Variable stores the necessary number of copies var count = 3; if (count === 2) { muffin.print(page); muffin.print(page); } if (count === 3) { muffin.print(page); muffin.print(page); muffin.print(page); }<p>In general, the approach is working, but no one writes programs like that. Imagine a program that can make one hundred copies of one page.</p>
2 // Variable stores the necessary number of copies var count = 3; if (count === 2) { muffin.print(page); muffin.print(page); } if (count === 3) { muffin.print(page); muffin.print(page); muffin.print(page); }<p>In general, the approach is working, but no one writes programs like that. Imagine a program that can make one hundred copies of one page.</p>
3 <p>To make execution of these repeated commands convenient, there are loops in programming languages. For example, the loop for:</p>
3 <p>To make execution of these repeated commands convenient, there are loops in programming languages. For example, the loop for:</p>
4 for (var i = 0; i &lt; count; i = i + 1) { // repeated commands }<p>If we write the loop for like this, the actions inside curly brackets will be executed count times. We will postpone careful consideration of the contents of for until the next task, but for now we’ll see how it works.</p>
4 for (var i = 0; i &lt; count; i = i + 1) { // repeated commands }<p>If we write the loop for like this, the actions inside curly brackets will be executed count times. We will postpone careful consideration of the contents of for until the next task, but for now we’ll see how it works.</p>
5 <p>Modify the driver so that you can control the number of copies with the variable count.</p>
5 <p>Modify the driver so that you can control the number of copies with the variable count.</p>
6 <p>By the way, in the “<a>Conditions</a>” chapter, we looked into strict (===) and approximate (==) equality. Why is it that strict equality is used in the example above?</p>
6 <p>By the way, in the “<a>Conditions</a>” chapter, we looked into strict (===) and approximate (==) equality. Why is it that strict equality is used in the example above?</p>
7 <p>Because such a comparison helps to avoid errors. For example, this comparison will work if we use approximate equality:</p>
7 <p>Because such a comparison helps to avoid errors. For example, this comparison will work if we use approximate equality:</p>
8 if ('003' == 3) { // … }<p>The same check using === will not work, because line '003' is not the same as number 3. In our program, the number of copies is written with a number and in all comparisons, we expect a number. Therefore, we use strict equality to make sure that inappropriate values do not slip into our program.</p>
8 if ('003' == 3) { // … }<p>The same check using === will not work, because line '003' is not the same as number 3. In our program, the number of copies is written with a number and in all comparisons, we expect a number. Therefore, we use strict equality to make sure that inappropriate values do not slip into our program.</p>
9 <p>Using strict equality is good practice. Use it and nothing else in all cases where possible.</p>
9 <p>Using strict equality is good practice. Use it and nothing else in all cases where possible.</p>