Iterating over an array [< or >]


#1

Can some please explain why this does not work?:

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];

for ( var q = 0; q > languages.length; q++) {
console.log(languages[q]);
}


but it doesn't work when using this q < languages.length;.


#2

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];

for ( var q = 0; q <languages.length; q++) {
    console.log(languages[q]);
}

your code has no problem .. if you change > to < .... if still doesn't work .please refresh your browser.


#3

Think of a for statement has having a range that starts at some initial value, then increments to some maximum at the end of the range. When we set the initial value to some number that is at the lower end of the range, we can reason that it cannot be a small number, and also be greater than the range maximum at the same time.

var i = 0; i > array.length;

The conditional will never be met under these conditions. Changing to < makes the condition true for all values in the lower end of the range. When writing a for statement, be sure to use a conditional that will be true at least once (for most practical purposes).