FAQ: Loops - Looping through Arrays

Hello @system9287394816.

This will always return undefined the way you are using it. If you are trying to access an element of an array named vacationSpots you would do so using this syntax: vacationSpots[index].

1 Like

Oh I see it now. Thank you =]

1 Like

Incidentally, I included:

because the following will work, but is a very bad practice because I am using the same name as the pre-defined length property as a property of an object that has an array as its value. I’m showing you this only to illustrate that your previous syntax in your string interpolation could work if length were a property of an object that had an array as a value.

Not recommended, but it works:

const vacationSpots = {
  length: ['Aruba', 'Jamaica', 'Bermuda'] //naming this property something like destinations would be a much better choice
}

for(let i = 0; i < vacationSpots.length.length; i++) { //the first '.length' refers to the property of the vacationSpots object; the second '.length' returns the length of the array
  console.log(`I want to go to ${vacationSpots.length[i]}.`) //looks confusing especially to other programmers coming across it
}

Output:

I want to go to Aruba.
I want to go to Jamaica.
I want to go to Bermuda.

1 Like

Well when I went back earlier I altered the code and they didn’t return undefined but I didn’t use .length twice.
Is that something you would also do in different cases though? Not the .length specifically.

In the context of the exercise we are just accessing an array containing vacation spots, so vacationSpots[index] is the way to access a specific element of the array.

If vacation spots were an object instead made up of various properties including something more appropriately named like destinations or maybe countries, then we would access the array that had the country elements in it as illustrated above. For example:

const vacationSpots = {
  countries: ['Aruba', 'Jamaica', 'Bermuda']
}

for(let i = 0; i < vacationSpots.countries.length; i++) {
  console.log(`I would like to visit ${vacationSpots.countries[i]}.`)
}

You may not be familiar with objects yet. If you are currently learning about loops, you will get to the lesson/exercises on objects soon, and this will all make more sense.

1 Like

4 posts were split to a new topic: For item of array

I had same problem but change the answer to:
for(let i =0;i< vacationSpots.length;i++){
console.log(I would love to visit ${vacationSpots[i]});
}
and it mark me green check.
Important: when I posed the reply I sow the sign at the left side the “1” key on keyboard not printed. this is important to add before “I” and after"]".
martinmiciciday

This was a really good answer and explanation. Really appreciate it.

It’s missing the ` marks at the beginning and end of what you’re logging to console.

console.log(` I would love to visit ${vacationSpots[i]}`` );

Other than that, it’s correct.

EDIT: RTF Editor was being a bit feisty.

What’s wrong, please?

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let i = 0; i < vacationSpots.lenght; i++) {
  console.log('I would love to visit ' + vacationSpots[i]);
}

Let’s not dwell on the obvious.

1 Like

Thank you so much! :grinning: This helped me a lot!

Hi,
I had started learning JS from codecademy and been stopped due to the following block -

const vacationSpots = [“Bali”,“Paris”,“Tulum”];
for (let i = 0; i < vacationSpots.length; i++) {
console.log(“I would love to visit” + vacationSpots[i]);
}

I see the code runs successfully when i try in browser. But here I am not able to proceed. The error is
image
[Did you log the sentence and vacation spot to the console as specified?] - 'in case the image is not visible ’
Could anyone please help me out say what’s the issue please?

“I would love to visitBali”
“I would love to visitParis”
“I would love to visitTulum”

Missing white space to separate the two strings. Check also if the exercise specifies a full stop at the end.

Hello,

In this exercise I used this code;

for (let i = 0; i < vacationSpots.length; i++ ){
  console.log('I would love to visit ' + vacationSpots[i]);

And I see this result in the console;

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

But my code haven’t approved. When I click on the solution I see this;

for (let i = 0; i < vacationSpots.length; i++) {console.log('I would love to visit ' +[vacationSpots[i]])};

I see that I had to put additional brackets. But is mine wrong/bad prectice/risky?

Thank you.

Hi mtf,
Please follow the below code screenshot. I tried with whitespace and fullstop too. But still unable to proceed due to error. Although result is accurate.

Could you please help me out ??

That is a head-scratcher. I have no idea the point of that and can only hope the SCT isn’t looking for that pattern. Skip the full stop, we don’t see one in the instructions, but those brackets… Huh?

Try using the solution code after your reset the lesson. Let us know if it passes. This is something for the curriculum team to look into.

@lilybird, anybody over there willing to give this a look?

Hello, thanks for the reply. I tried the exercise with refreshed console. So it’s interesting; it worked, I mean my initial code worked. Also, I checked solution again, it doesn’t have extra brackets! I think it is because of some browser cache or something. I am not sure. I tested twice with refreshed console.

1 Like

Great! At least we know it’s not an SCT or solution issue. No telling what might have been going on but glad that resetting solved the problem.

2 Likes

For me, I am confused over how the console.log part of the code at the bottom can correlate with the [i].

const vacationSpots = [‘Bali’, ‘Paris’, ‘Tulum’];

for (let i = 0; i < vacationSpots.length; i++ )

{

console.log( 'I would love to visit ’ + vacationSpots[i])

}

As seen above, if i < vacationSpots.length, the loop repeats itself. I understand that segment, but how is the console.log part able to attach itself to the ‘for’ loop? In other words, how does the i++ in the ‘for’ loop correlate with the console.log sentence?