Backwards Loop Question

Hello! I'm just a little confused about the way a backwards loop works. The exercise given in the course is as follows:

let vacationSpots = ['Lahore', 'Amritsar', 'Delhi',];

for(let vacationSpotIndex = vacationSpots.length - 1; vacationSpotIndex >= 0; vacationSpotIndex–){
console.log('I would love to visit ’ + vacationSpots[vacationSpotIndex]);

If the stop condition is set to less than OR equal to 0, wouldn't this prevent the loop from running in the first place? The start condition is clearly greater than 0, thus satisfying at least one condition of the OR operator. However, the code seems to work just fine and in fact is checked off as correct. Could anyone please explain this to me?

while the condition of the loop evaluates to true, the loop runs.

the condition is set in such a way, that its while vacationSpotIndex is greater then or equal to zero, keep running, seems you got it the wrong way around

That's what I don't understand. The start condition is vacationSpots.length - 1 which is equal to 2. How could the loop run since 2 is greater than 0?

2 >= 0 is true, so the loop runs. The loop ends, when condition becomes false


Oh I think I’ve got it now. Thanks very much for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.