Skip to content

Instantly share code, notes, and snippets.

@ryan-w-moore
Created November 11, 2014 09:46
Show Gist options
  • Save ryan-w-moore/4e54dd733f098e780a37 to your computer and use it in GitHub Desktop.
Save ryan-w-moore/4e54dd733f098e780a37 to your computer and use it in GitHub Desktop.
Syntax, Cont'd...
var foo = 10;
var bar = 20;
if (foo < bar ) {
//console.log("foo is less than bar");
//console.log("and here's a second message");
//console.log("and here's a third message");
//console.log(foo);
//console.log(foo,bar);
//console.debug("foo is less than bar");
//console.info("foo is less than bar");
//console.warn("foo is less than bar");
console.error("foo is less than bar");
}
// When we realize rather quickly how valuable code-reuse, even within a script is to the developer.
// Logic Example:
for every link {
statement one
statment two
}
// I. WHILE LOOP (The Most Basic Kind of Loop):
var a = 1;
while ( a < 10 ) { // is a less than 10? yes thats true, continue to the next line...
console.log(a);
}; // <-- WARNING! INFINITE LOOP!
// Thus, to fix the infinity of this loop, we increment the a variable by one with each iteration of the loop:
while ( a < 10 ) {
console.log(a);
a++;
}
// II. The "DO..WHILE LOOP"
// "WHILE LOOPS are much more common than these, DO...WHILE LOOPS, b/c it's always considered best practice to check the condition
// ...before entering the loop. And it's more readable that way too!
var a = 100;
do{
console.log(a); // the block will always iterate at least once! Even if the var a is set to 100, it loops at least once before checking the condition.
a++;
} while ( a < 10 );
// In many loops a pattern emerges:
// Take this WHILE LOOP:
var i = 1; // <-- Set up the Index...
while ( i < 10 ) { // <-- Check the Condition...
// do stuff
// do stuff
// do stuff
i++; // <-- Increment the Index/Counter...
}
// No matter how many statements are inside the loop we always have the basic skeletal structure of the loop as above...
// III. The "FOR LOOP" :
// Set up an "Index" or a "Counter" variable.
// There is another, more professional and correct term for the i var, check crockford's 2011 lecture video.
// Now, because there are always the three main features inherent in any loop:
// 1. Set up the Index..
// 2. Check the Condition...
// 3. Increment the Index/Counter...
// There is a shorter way to create this type of loop, The "FOR LOOP"
for ( var i = 1; i < 10; i++ ) {
// do stuff
// do stuff
// do stuff
// etc...
}
// Another way of explaining the above:
// for (setupindex; checkCondition; incrementCounter)
// IV. BREAK:
// BREAK Jumps us out of the loop.
// For Example:
for ( var i = 1; i < 5000; i++ ) {
// do stuff
// do stuff
if ( i == 101 ) {
break;
}
// do stuff
}
// break jumps out of the loop
// V. CONTINUE:
//
for ( var i = 1; i < 5000; i++ ) {
// do stuff
// do stuff
if (i % 5 == 0) {
continue; // done with this iteration
}
// do second set of stuff
// do second set of stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment