Skip to content

Instantly share code, notes, and snippets.

@stevencombs
Last active December 22, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevencombs/6400160 to your computer and use it in GitHub Desktop.
Save stevencombs/6400160 to your computer and use it in GitHub Desktop.
Various Javascript control flow examples.
// 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