Introduction to ‘for’ loops in js

I For loops: The Basics

i. Why use for loops? 1/13

console.log("1");
console.log("2");
console.log("3");
console.log("4");
console.log("5");

ii. First for loop 2/13

for (var counter = 1; counter < 11; counter++) {
	console.log(counter);
}

iii. Starting the for loop 3/13

for (var i = 5; i < 11; i = i + 1){
	console.log(i);
}

iv. Ending the for loop 4/13

for (var i = 4; i < 24; i = i + 1) {
	console.log(i);
}

v. Controlling the for loop 5/13

for (var i = 5; i < 51; i+= 5) {
	console.log(i);
}

II Practicing loops!
i. How does it work? 6/13

for (var i = 8 ; i < 120; i+= 12) {
	console.log(i);
}

ii. Practice counting down 7/13

for (var i = 10; i >= 0; i-= 1) {
	console.log(i);
}

iii. Last practice for loop 8/13

for ( var i = 100; i > 0; i-= 5) {
    console.log(i);
}

III Arrays and Loops
i. Meet arrays 9/13

var junk = ["games", "rubbish", 4, 5];
console.log(junk);

ii. Array positions 10/13

var junkData = ["Eddie Murphy", 49, "peanuts", 31];
console.log(junkData[3]);

iii. Loops and arrays I 11/13

var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "New Delhi"];

for (var i = 0; i < cities.length; i++) {
    console.log("I would like to visit " + cities[i]);
}

iv. Loops and arrays II 12/13

var names = ["Sunakshi", "Saksham", "Sam", "Susie", "Sargam"];
for (var i = 0; i < names.length; i++) {
    console.log("I know someone called " + names[i]);
}

v. Conclusion 13/13
Save and Submit

Advertisements

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