Commit c341638e authored by MohammadAli Keshavarz's avatar MohammadAli Keshavarz

Loops and iteration

parent 914b11a1
......@@ -348,7 +348,7 @@ console.log(jane);
/*****************************
* Objects and methods
*/
/*
var john = {
firstName: 'John',
lastName: 'Smith',
......@@ -362,5 +362,48 @@ var john = {
};
john.calcAge();
console.log(john);
*/
/*****************************
* Loops and iteration
*/
// for loop
for (var i = 1; i <= 20; i += 2) {
console.log(i);
}
// i = 0, 0 < 10 true, log i to console, i++
// i = 1, 1 < 10 true, log i to the console, i++
//...
// i = 9, 9 < 10 true, log i to the console, i++
// i = 10, 10 < 10 FALSE, exit the loop!
var john = ['John', 'Smith', 1990, 'designer', false, 'blue'];
for (var i = 0; i < john.length; i++) {
console.log(john[i]);
}
// While loop
var i = 0;
while(i < john.length) {
console.log(john[i]);
i++;
}
// continue and break statements
var john = ['John', 'Smith', 1990, 'designer', false, 'blue'];
for (var i = 0; i < john.length; i++) {
if (typeof john[i] !== 'string') continue;
console.log(john[i]);
}
for (var i = 0; i < john.length; i++) {
if (typeof john[i] !== 'string') break;
console.log(john[i]);
}
// Looping backwards
for (var i = john.length - 1; i >= 0; i--) {
console.log(john[i]);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment