0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>It’s time to break out of this endless series of wins!</p>
1
<p>It’s time to break out of this endless series of wins!</p>
2
<p>The program already knows how to accumulate misses in the misses variable. All we have left to do is teach it how to stop the loop after reaching three misses. But where in the loop should this condition be checked? In the beginning, the middle or the end?</p>
2
<p>The program already knows how to accumulate misses in the misses variable. All we have left to do is teach it how to stop the loop after reaching three misses. But where in the loop should this condition be checked? In the beginning, the middle or the end?</p>
3
<p>Here is the current order of the commands in the loop:</p>
3
<p>Here is the current order of the commands in the loop:</p>
4
<ol><li>We get the result of the shot.</li>
4
<ol><li>We get the result of the shot.</li>
5
<li>Check that the shot result is less than zero. If this is the case, then go to step three, otherwise go to step four.</li>
5
<li>Check that the shot result is less than zero. If this is the case, then go to step three, otherwise go to step four.</li>
6
<li>Record the miss in the console and increase the miss counter by one.</li>
6
<li>Record the miss in the console and increase the miss counter by one.</li>
7
<li>Increase total result total and log the shot result in the console.</li>
7
<li>Increase total result total and log the shot result in the console.</li>
8
</ol><p>We need the following:</p>
8
</ol><p>We need the following:</p>
9
<ul><li>A check for the total number of misses must be completed at every iteration of the loop.</li>
9
<ul><li>A check for the total number of misses must be completed at every iteration of the loop.</li>
10
<li>When the three-miss mark is reached, the game must<em>immediately</em>stop.</li>
10
<li>When the three-miss mark is reached, the game must<em>immediately</em>stop.</li>
11
</ul><p>Therefore, the total number of misses must be checked at the very beginning of the loop, even before the shot is done and its result is obtained.</p>
11
</ul><p>Therefore, the total number of misses must be checked at the very beginning of the loop, even before the shot is done and its result is obtained.</p>
12
<p>We need a command that will interrupt the execution of the loop.</p>
12
<p>We need a command that will interrupt the execution of the loop.</p>
13
<p>This command in JavaScript is the break operator. Add to the beginning of the loop a check for the number of misses and as soon as we get three misses, we stop the loop.</p>
13
<p>This command in JavaScript is the break operator. Add to the beginning of the loop a check for the number of misses and as soon as we get three misses, we stop the loop.</p>
14
<p>Similarly to the loop interrupt operator break, there is an operator for a quick jump to the next iteration of the loop continue, but it is used extremely rarely, since it makes it difficult to read the code and understand how the loop works in general. Using continue without a need for it is usually considered an antipattern.</p>
14
<p>Similarly to the loop interrupt operator break, there is an operator for a quick jump to the next iteration of the loop continue, but it is used extremely rarely, since it makes it difficult to read the code and understand how the loop works in general. Using continue without a need for it is usually considered an antipattern.</p>
15
<ul><li>Inside while, command continue “rewinds” the program immediately to the beginning of the<em>next</em>iteration.</li>
15
<ul><li>Inside while, command continue “rewinds” the program immediately to the beginning of the<em>next</em>iteration.</li>
16
<li>Inside for, command continue “rewinds” the program to the addition part of the<em>current</em>iteration, after execution of which the<em>next</em>iteration of the loop begins.</li>
16
<li>Inside for, command continue “rewinds” the program to the addition part of the<em>current</em>iteration, after execution of which the<em>next</em>iteration of the loop begins.</li>
17
</ul>
17
</ul>