Solution not accepted for exercises

In many exercises I have come across, my solution is not accepted, which, as far as I know, is correct.

  • Error code-

Did you make the start condition of the for loop var i = vacationSpots.length?

Learn JavaScript Loops

The task says this-

We need to make three changes to our for loop:

Edit the start condition (var i = 0), to set i equal to the length of the vacationSpots array.

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.

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

My code-

var vacationSpots = ['Spain', 'New York', 'Barcelona'];

for (var k = vacationSpots.length; k >=0; k--) {
  console.log('I would love to visit ' + vacationSpots[k]);

And the **result** is this- >I would love to visit undefined >I would love to visit Barcelona >I would love to visit New York >I would love to visit Spain

By the way, there is no problem with the first line saying undefined, as it was supposed to be corrected in the next task.

  • Error-

Did you a for loop that loops over the myPlaces array?

Java Script loops 5/7
The task says this-

Write a for loop that iterates through each item in myPlaces and logs out each place.

My Code-

var myPlaces=['Goa','Spain','New York'];

var friendPlaces=['USA','India','China'];


New York

I have also reported this in the report a bug section.
Also, is anyone else out there going through the same???

The code looks great. I suspect the only problem here is that Codecademy is specifically looking for i and not the k you used.

Your second example also looks fine except for a = 0 is not explicitly declared as a new variable: var a = 0.

But does it make any difference to the code?, by changing a variable name???

Not at all, in the real world. I’m just saying Codecademy’s editor may require it under the hood as part of the error-checking mechanism. If that’s the only problem then I would say it is indeed worth a bug report. Although, the opposite case could be made: since the instructions tell you what to name the variable, you’re not following instructions correctly. :grimacing:

:thinking: oh…
by the way thanks for the help!

1 Like

You’re producing an error by setting var k = vacationSpots.length;. This is responsible for producing the first ‘undefined’ vacation destination.

The length property of an array is one larger than the index of the last element.

EDIT: never mind, I see that the next step covers this.

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