# Output the elements in vacationSpots backwards

I was trying to write the following code in a way that the console would output the elements in `vacationSpots` backwards (ie. `"I would love to visit Tulum","I would love to visit Paris", "I would love to visit Bali"`.

``````const vacationSpots = ['Bali', 'Paris', 'Tulum'];

for (let i = 2; i <= vacationSpots.length; i--) {
console.log(`I would love to visit \${vacationSpots[i]}`);
};
``````

However, whenever I run the code, the console returns the following:

Could someone please explain what this error means, and how I should fix my code? Thank you so much!

There are three parameters that have to agree with one another for the loop to play out. Which parameter above creates a disagreement?

@mtf When you write â€śparameterâ€ť do you mean that everything between the semi-colons are a parameter -and that there will always be three parameters in a `for` loop?

In that case, would it be the `i--` that would be disagreeing with the other parameters?

Yes; even if they are absent, the `;`'s are still required to demarcate them.

``````for (;;) {}
``````

Of course in this construct we would be controlling the show from someplace, obviously, or it wouldnâ€™t work.

The basic requirements of a `for` loop are threefoldâ€¦

1. An initial state;
2. a run while conditional; and,
3. a change of state for the next iteration.

Thank you, that makes sense actually! The three-part explanation really helped.

So then, in the case of the backward code (without correction), would it mean that if `"Tulum"` is the initializer, then for every element before `"Tulum"`, subtract one? â€¦Which wouldnâ€™t be possible because `"Bali"` has an index of `zero`, and there is no index with a value of `-1`?

The direction is determined by all three parameters. That is why they all have to be in agreement with one another.

• When going from left to right we set an upper bound condition and increment the iteration variable. The iteration is initiated at the lowest value.

• When going to from right to left we set a lower bound condition and decrement the iteration variable. The iteration is initiated at the highest value.

Thatâ€™s really helpful, thank you so much for the guidance!!

I think I understand how this whole for-loop iteration works better now, and will give my code another try

1 Like

Youâ€™re welcome! Keep us posted of your progress even if just to say youâ€™ve moved on.

1 Like

Hi @mtf! I ran the following code in the CodeCademy console, however, there was no visible output. I was wondering why?

I wanted my code to go from right to left, and because thereâ€™s only three items in the array, I believe the highest initialized value would be `2`. As for the â€ślower bound conditionâ€ť, I interpreted that as using the `<` sign, and I did decrement the iteration variable to `i--`.

``````const vacationSpots = ['Bali', 'Paris', 'Tulum'];

for (let i = 2; i < vacationSpots.length; i--) {
console.log(`I would love to visit \${vacationSpots[i]}`);
};
``````

So, I was wondering if something in my code went haywireâ€¦or how else should I be able to see the output? Thank you for your help again!!

You still havenâ€™t answered the question about what does not agree. What does not agree?

Would it be the middle parameter that disagrees with the other two?

Spot on. Now how will we get it to agree?

Hmmâ€¦ well, following your advice, since weâ€™re going from right to left we should

for the lower bound condition, would we actually use the â€ś>=â€ť sign, so that `i >= vacationSpots.length;`(and that way, agree with the other two parameters)?

A lower bound means that all values greater will be allowed.

``````for (x = 10; x > 0; x--) {
console.log(x)
}
``````

Itâ€™s important to realize that the thing with any size is the array, so the length would be used in the initial state, not the conditional, which is the lower bound.

@mtf, thank you!! That explanation really helps.

Itâ€™s just that in the example provided by the lesson, `.length` was in the conditional, so thatâ€™s where I thought it would belong. I guess it makes sense that if weâ€™re trying to output something backward, that would make sense

Hereâ€™s my newly improvised code (all thanks to you!):

``````for (let i = vacationSpots.length; i >= 0; i--) {
console.log(`I would love to visit \${vacationSpots[i]}`);
};
``````

and when I printed this to the console, it worked!!

``````I would love to visit undefined
I would love to visit Tulum
I would love to visit Paris
I would love to visit Bali
``````

The only thing is, I get this undefined statement at the beginning. I am wondering why?

1 Like

Recall that the last element will be at the index that is one less than the length.

1 Like

Youâ€™re right!! Thank you for reminding me haha I totally get it now!