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 :grin:

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!! :+1:

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--) {

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 :sweat_smile:

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!

Greatly appreciate your help :+1:

1 Like