For loops,Backwards

let vacationSpots = [‘Mozambique’, ‘Thailand’, ‘Bolivia’];

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

The output starts as “I would love to visit Bolivia” etc.

My Question is since vacationSpotIndex =vacationspots.length -1,
shouldn’t it be starting with"I would love to visit Thailand"

Thanks.

no, your array has 3 items in it, so the length is 3

however, the indexes of the 3 items in your array are: 0, 1, 2

given indexes start from zero, so without minus one, you would first get undefined (given there is nothing at index 3)

2 Likes

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