Lab on while loops in JS
Goals
- Practice with JavaScript's:
- while and do-while loops
- conditionals
- mod operator (%)
- dynamically creating webpage context (innerHTML)
Task 1: Count up and down
Create a folder on your Desktop, named lab8. Download the starting web page, countUp.html into lab8 folder.
Use that file to add your javascript code for the following tasks.
Task 1A
Ask the user for a positive integer. Print on the page all the integers from 0 to that number (included), one per line.
Now add some code to make sure the number you get from the user is indeed a positive number: if you get a negative (or zero) number, prompt them again, until you get a valid (i.e positive) number.
Task 1B
Update your javascript code in task 1A to prevent the number 3 from printing, i.e. print all positive integers up to the user's number, except for 3.
With a small change to your code, you can prevent all multiples of 3 from printing. Do so!
Now, generalize this task a little more: allow the user to tell you (through a prompt) which number's multiples should not be printed. This number should be positive and less than the first received number.
Task 1C
Write some javascript code to ask the user for a positive integer. Write on the page, each number from the user's input to -3, one number per line.
Task 1D
Modify your code in the previous task, so no number less than 4 is printed.