Introduction to ‘while’ Loops in Javascript

I ‘While’ Loops: The Basics

i. While we’re at it 1/11
Save and Submit
ii. While syntax 2/11

var understand = true;
while(understand === true  ){
	console.log("I'm learning while loops!");
	understand = false;
}

iii. A fellow of infinite loops 3/11

understand = true;
while(understand ===  true){
	console.log("I'm learning while loops!");
	understand = false;
}

iv. Brevity is the soul of programming 4/11

var bool = true;
while(bool){
    console.log("Less is more!");
    bool = false;
}

II More Practice with Loops!
i. Practice makes perfect 5/11

var count = 0
var loop = function(count){
	while(count < 3 ){
		console.log("I'm looping!");
		count++;
	}
}
loop(count);

ii. Solo flight 6/11

var solo = true
var soloLoop = function() {
    while(solo) {
        console.log("Looped once!");
        solo = false;
    }    
}
soloLoop(solo);

iii. Mid-lesson breather 7/11
Save and Submit
III More with ‘While’
i. When to ‘while’ and when to ‘for’ 8/11

var y = 1
    while(y < 5) {
        console.log("I am looping");
        y++
    }
for (var i = 1; i < 2; i++) {
    console.log(y + 3);
}

ii. The ‘do’ / ‘while’ loop 9/11

var loopCondition = false;
do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition);

iii. To learn it, you gotta ‘do’ it 10/11

var x = false;
var getToDaChoppa = function(){
  // Write your do/while loop here!
 do {
     console.log("My first do loop, but it is " + x)
 } while(x);
}
getToDaChoppa();

v. Review 11/11

var age = 10;
while(age < 51) {
    console.log("I am still young at " + age);
    age += 10;
}

for (var i = 1; i < 5; i += 1) {
    console.log("Am I old at " + age + " ?");
    age += 10
}
var age = false

do {
    console.log("I am dead at 100" );
} while(age);
Advertisements

One thought on “Introduction to ‘while’ Loops in Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s