Last active
December 22, 2015 01:59
-
-
Save stevencombs/6400160 to your computer and use it in GitHub Desktop.
Various Javascript control flow examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript Getting Started with Programming | |
// A Codecademy Javascript (When to 'while' and when to 'for') assignment | |
// Dr. Steven B. Combs, coding novice | |
// For loop - use to repeat code a specific number of times | |
for(var i = 0; i < 3; i++){ | |
console.log("Hello World!"); | |
} | |
// While loop - use to repeat code until a specific condition is met | |
var understand = true; | |
while(understand===true){ | |
console.log("Hello World!"); | |
understand = false; | |
} | |
// While loop as a function variation | |
var HelloWorld = function(){ | |
do { | |
console.log("Hello World!"); | |
cond = false; | |
} while (cond===true); | |
}; | |
HelloWorld(); | |
// Switch - simplifies code when multiple options available | |
var pet = prompt("What type of pet do you have?","Type your lunch choice here"); | |
switch(pet){ | |
case 'dog': | |
console.log("Man's best friend!"); | |
break; | |
case 'cat': | |
console.log("Feisty felines!"); | |
break; | |
case 'fish': | |
console.log("Not much company, but nice to look at."); | |
break; | |
case 'snake': | |
console.log("Slithering friend!"); | |
break; | |
default: | |
console.log("A " + pet + " I am unfamiliar with. Tell me about it."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment