Looping Through Arrays Query


#1

Hi All

I was doing exercise 5 (Looping through Arrays) and the code is as follows:

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

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

Which prints:

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

While playing around, I decided I just wanted to see if I could have the console add a few extra exclamation marks for Paris only so drafted this code:

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

if (vacationSpots[i] = 'Paris') {
console.log(`I would love to visit ${vacationSpots[i]}!!!`);  

}
};

1: It doesn’t work as I just wanted the line that says “I would love to visit Paris” to have some additional exclamation marks at the end; also

  1. It prints the following to the console and I can’t figure out why:

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

I don’t mind that my code didn’t work in the first instance but I am annoyed that I can’t figure out why it prints it like the above in the console. I can’t follow the code to figure out why it prints that out. I thought it would print:

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

Could you guys help me with the initial code and secondly, maybe explain why my additional code prints “Paris” so much?

Thanks

Link to exercise:

https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/for-loops-with-arrays?action=resume_content_item


#2

i think your problem lies here. This should be 3x “=”.

if (vacationSpots[i] === ‘Paris’)


#3

Thanks!

With that change it now prints:

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

Is there a way to do it without the first “Paris!” sentence appearing?


#4
for (let i = 0; i < vacationSpots.length; i++) {
    console.log(I would love to visit ${vacationSpots[i]}!);   <---   This line.

    if (vacationSpots[i] = 'Paris') {
        console.log(`I would love to visit ${vacationSpots[i]}!!!`);  
    }
}

The reason that paris is printed twice is because this line:
console.log(I would love to visit ${vacationSpots[i]}!);
is not in a if statement.

I think you know how to make it so its printing the right one without printing the other.


#5

Hrmm…

I have moved to this style:

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

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

if (vacationSpots[i] === 'Paris') {

return ‘Paris’ === ‘Paris!!!’;

}
};

…but still no luck. I’m not sure if I have the knowledge to do this yet from the lessons I have covered?


#6

Well i am not gonna do it for you but i can give you a direction on where to start looking.

Here is some information on if statements and how you use them.

Also here you can find on how the comparison operators work.


#7

Thanks for the pointers! I got it in the end rather easily:

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

for (let i = 0; i < vacationSpots.length; i++) {
if (vacationSpots[i] === ‘Paris’) {
console.log(I would love to visit PARIS!!!)
} else {
console.log(I would love to visit ${vacationSpots[i]}!);
}
};

:slight_smile: