Skip to content
jfraboni edited this page Jan 4, 2015 · 3 revisions

Code and Quiz

Code a little

Take the quiz

The power of computation is evident in the ability to process data. Most applications will at some point need to process collections of data.

Loops

Generally speaking, we call a bunch of things a list or a collection, and we call the things that make up a list or collection, elements or items. Specific programming languages have specific implementations of collections, and in JavaScript, we have the Array (indexed lists) and the Object (key / value pairs).

The concept of looping over the elements in a collection is called iteration. We iterate over each element in a collection to do something with each element.

Say for example, you have a list of friends, and you want to print their names and phone numbers. You could do someting like this:

console.log(myFriends[0].name + ' : ' + myFriends[0].phone);
console.log(myFriends[1].name + ' : ' + myFriends[1].phone);
console.log(myFriends[2].name + ' : ' + myFriends[2].phone);

...but what if you had 1,500 friends in that list? What if you didn't know the length of the list?

All programming languages have features that allow us to process or iterate over lists, take out each element in the list, do something with that element, then move on to the next element. Simply for the sake of effective code maintainance, its is much more effecient to use a loop than to hard-code accessing and processing every element in an Array. Our requirements could be much more easily expressed with the following loop:

for (var i = 0; i < myFriends.length; i++) {
	console.log(myFriends[i].name + ' : ' + myFriends[i].phone);	
}

A much more elegant solution, but leaning on the language feature of the for loop, we do not have to hard code any element position values, we don't have to repeat ourselves by writing code for every element in the Array, and we don't need to even know how many items are in the Array.

There's many options for looping in JavaScript; let's have a look at some of them.

The for Loop

The for-in Loop

The while Statement

While statements define a block of code that will excute over and over while a condition is true. They are essentially a loop.

####Syntax

Defined by the while keyword, followed by a condition within parenthesis, followed by a code block:

// while : while x is greater than 0, the body of the while loop will execute //
var x = 5;
while (x > 0) {
    console.log(x);
    x--;
}

The do-while Statement

Basically, another version of while, except that with the do-while statement, the code block is always executed at least once, and if the trailing conditional check equates to true, the block is executed again, until the conditional statement equates to false.

####Syntax

We use the keyword do, followed by a code block, followed by the keyword while, finally followed by a conditional check:

// do while : while i is less than 5, body of the do-while loop will execute //
var i = 0;
do {
    console.log(i);
    i++;
} while (i < 5);

© 2014-2015 Operation Spark