Help with running a for loop backwards exercise



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Only an error prints out in the console and I have no idea why.
var vacationSpots = [‘Green Hill Zone’, ‘Kanto Region’, ‘Hyrule’];

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

Errors in the console:
for (var i = (vacationSpots.length - 1); i > 0, i–);
SyntaxError: Unexpected token )
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

I’ve tried many things but to no success. Codeacademy seems to think I passed the exercise as well when nothing has been printed out as well which makes it weirder.


Replace this line with your code.

<do not remove the three backticks above>


Hi @robotmanatee,

Firstly, you have an additional semicolon ; in the code, it stops the code from moving forward to execute the code block. Can you find it?

for (var i = (vacationSpots.length - 1); i > 0, i–);  //<=== Beware of this extra semicolon
console.log('I would love to visit ' + vacationSpots[i]);

Secondly, after you have deleted the semicolon, have a look on the for loop syntax:

for (start condition; stop condition ; iterator )

Your code:

for (var i = (vacationSpots.length - 1); i > 0, i–)

Notice where you should place your semicolon? You got one right but the second one wrong, you need to replace the comma , into semicolon ;

Thirdly, the for loop conditions:

Copied from the instruction:

To do this, we’ll need to edit the code between the for loop’s parentheses:

  • The start condition should set i to the length of the array.
  • The stop condition should end when i is 0.
  • The iterator should subtract 1 each time, which is done with i–.

Your start condition in the for loop is correct, but the bolded ones need to be tweak a little:

for (var i = (vacationSpots.length - 1); i > 0, i–)

From the lesson:

Then, set the stop condition ( i < vacationSpots.length) to stop when i is greater than or equal to 0.

Finally, change i++ to i- - to subtract 1 from i each loop.

1.For stop conditon, what is the operator used when i is greater than or equal to 0?
2.For iterator, it’s i - -

Change all those three errors and your code will work then.

Cheers :slight_smile:


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