Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Printing only odd pages
Look at how quickly you are getting along! Let’s remember what else a printer should be able to do:
The new printer is simple enough, it should be able to print
all the pages in directandreverse order, print even and odd pages andmake copies of one page.
All we have to do is print even and odd pages. Printing only odd pages is easy:
- You need to start printing from the first page.
- After each iteration of the loop, you need to increase the number of the current page by
2
, and not by1
.
Perhaps you already noticed that in loops, an increase or decrease of variables by some number is regularly used. And we constantly have to write cumbersome structures, such as page = page + 1
. Fortunately, JavaScript has several convenient operators that allow us to shorten the code. Here they are:
Name | Example | Analog |
---|---|---|
Increment (increase by one) | i++ | i = i + 1 |
Decrement (decrease by one) | i-- | i = i - 1 |
C-c-combo! | i += 2 | i = i + 2 |
You can combine not only addition, but also other mathematical operations: subtraction -=
, multiplication *=
, division /=
and calculating the remainder %=
. For example, i *= 10
will do the same as i = i * 10
.
- script.js
Thanks! We’ll fix everything at once!
Comments