Skip to content

Instantly share code, notes, and snippets.

@annajmorton
Created June 2, 2016 14:40
Show Gist options
  • Save annajmorton/8699197e21c994c025e4a7798dd67420 to your computer and use it in GitHub Desktop.
Save annajmorton/8699197e21c994c025e4a7798dd67420 to your computer and use it in GitHub Desktop.
Exercise Solutions - Break and Continue

Break and Continue

Please follow the instructions below.

  1. Create an HTML file named break_continue_js.html within the ~/vagrant-lamp/sites/codeup.dev/public folder on your Mac.
  2. Use Math.floor((Math.random()*50)+1) to generate a random number between 1 and 50. If the number is not odd, keep getting a new random number till it is odd.
  3. Use console.log to log all the odd numbers from 1 to 50, except the random odd number you generated, by using break and continue. Try to match the output shown below (your random number will be different).
  4. Finally, save your work, commit the changes to your git repository, and push to GitHub.

Your output should look like this (but a different random odd number to skip):

    Random odd number to skip is: 27
    Here is an odd number: 1
    Here is an odd number: 3
    Here is an odd number: 5
    Here is an odd number: 7
    Here is an odd number: 9
    Here is an odd number: 11
    Here is an odd number: 13
    Here is an odd number: 15
    Here is an odd number: 17
    Here is an odd number: 19
    Here is an odd number: 21
    Here is an odd number: 23
    Here is an odd number: 25
    Yikes! Skipping number: 27
    Here is an odd number: 29
    Here is an odd number: 31
    Here is an odd number: 33
    Here is an odd number: 35
    Here is an odd number: 37
    Here is an odd number: 39
    Here is an odd number: 41
    Here is an odd number: 43
    Here is an odd number: 45
    Here is an odd number: 47
    Here is an odd number: 49

###solution:

"use strict"

var randoskip=0;

while(randoskip%2==0){
	randoskip = Math.floor((Math.random()*50)+1);
}
for (var i = 1; i <= 50; i++) {
	if (i == randoskip) {
		console.log("Yikes! skipping number: "+i);
		continue;
	}
	if (i%2 != 0) {
		console.log("Here is an odd number: "+i);
	}		
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment