Write a sketch to display numbers from 0 to 20 on the screen. The output should be produced using the text()
function. Use the previously provided textHeight()
function to place the text on different lines.
Each line should be like:
Number 1
Number 2
Number 3
Use a for loop to display the 20 numbers on 20 separate lines.
Now add another loop to your code that displays the numbers in reverse order from 20 down to 0. (So now there are two lists of numbers on your screen. Same output requirement for this as for the first loop).
Now add another loop to your code that displays only the even numbers from 0 to 20. (Now there are three lists of numbers on your screen. Same output requirements for above). Hint: Remember that each of the 3 statements in the for loop parenthesis section are full statements. They can be more complex than we have shown so far.
In previous assignments, you have probably been using the loop counter to keep track of which line you are on for display purposes. If you have not or don’t understand, PLEASE ask Ms Elwell or Mr Helmke. That won’t work the same way for the reverse number count or the even numbers because your line count won’t go down the screen. Try it and see?
An alternate way you can do it is like this:
1 2 3 4 5 | var line = 0; for(var i=0; i < 5; i++) { text("num " + i, 200, 200 + line*textHeight()); line = line + 1; } |
Note this is an example and not quite how you are supposed to do this assignment…
Name your sketch NumberLoop1
Go back to Loops 2