3/8,1+2 Ques HELP


#1

i type everything, the code run but it says:Did you use ‘let vacationSpotIndex = 0;’ as your start condition?

https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/for-loop?action=lesson_resume&course_redirect=introduction-to-javascript



Replace this line with your code. 


let vacationSpots = [‘Kuala Lumpur’, ‘london’ , ‘spain’]

for (let vacationSpotsIndex = 0; vacationSpotsIndex < vacationSpots.length; vacationSpotsIndex++) {
console.log(vacationSpots[vacationSpotsIndex])
}

/*
let vacationSpots = [“Kuala Lumpur”, “london”, “spain”];

for (let vacationspotsIndex = 0; vacationSpotsIndex < vacationSpots.length; vacationSpotsIndex++) {
console.log(vacationSpots[VacaSpotsIndex]);
}
*/


#2

Hi @aaronjoseph2001,

The error it throws:

Did you use ‘let vacationSpotIndex = 0;’

Compared that to your code, it’s the spelling, remember if you change the start condition, you will have to change the variable spelling for the rest in your code.

It’s a small trick/trap that the exercise has, as it’s looking for vacationSpotIndex :wink:

Change all that, should be good to go.


#3

@codexthon
i though codecademy got a spelling error
i forgot about that :sweat_smile::sweat_smile:


#4

@codexthon need your help again

i copy also can not

this is the code


let vacationSpots = ['Kuala Lumpur', 'london' , 'spain']

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


//console.log('I would love to visit ' +
  //         vacationSpots[vacationSpotIndex]);
console.log('I would love to visit ' + vacationSpots[vacationSpotIndex]);



/*
let vacationSpots = ["Kuala Lumpur", "london", "spain"];

for (let vacationspotsIndex = 0; vacationSpotsIndex < vacationSpots.length; vacationSpotsIndex++) {
  console.log(vacationSpots[VacaSpotsIndex]);
}
*/

#5

Ques 3 same link same things :roll_eyes:


#6

Hi @aaronjoseph2001,

A favor needed here, can you clean the code and delete the experimental comments in the code? Because from what you posted, the code is a bit jumbled to look at and makes it harder for anyone to analyze.

From your code, I’m assumed you’re in Step 2?
The exercise want you to print out the sentences accordingly:

Instruction:
add text to the console.log() statement

So that it will print like this:

I would love to visit .....
I would love to visit .....
I would love to visit .....

How would you achieve that? Check the placement of your console.log and notice you currently have two console.log there, only one console.log is needed as it was asked to add text into the existing console.log

:slight_smile:


#7

@codexthon

here the clearer code

let vacationSpots = ['Kuala Lumpur', 'london' , 'spain']

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

i try what you said but it didn’t work also and when i press run the thing seems loading forever :sweat_smile:


#8

There is a problem, for sure. This expression never increments the index.

i++
i += 1
i = i + 1

All of the above will increment the index pointer… So,

vacationSpotIndex++

#9

Hi @aaronjoseph2001, in your previous code before you tidy it up, your code:

let vacationSpots = ['Kuala Lumpur', 'london' , 'spain']

for (let vacationSpotIndex = 0; vacationSpotIndex < vacationSpots.length; vacationSpotIndex++) { 
  console.log(vacationSpots[vacationSpotIndex]);
}

You had used vacationSpotIndex++ as the incremental condition, which is correct, why change it then? :thinking:


For more clarification, what @mtf mentioned about:

Works that same way, when you want the i increase by 1. For that, i++ is the shortest way of writing it, wherease, i = i + 1 is the longest way of writing that code, all three work the same.

Bear in mind though, i++ will not work if you wish to increment the i by >1, (by 2, 3, 4, so on). It only stands for short hand increase i by 1.

The same will imply for if you wish to decrease i by 1, you can write:

i--
i -= 1
i = i - 1

by writing just an expression without saving it back to i, i is not incremented:

i + 1

That’s why for your code, vacationSpotIndex always equal to 0, even code has run vacationSpotIndex + 1, the value of the expression is lost without saving back to vacationSpotIndex, vacationSpotIndex will always < vacationSpots.length and your loop continue to loop forever until it crashes (infinite loop)


After you amend your loop back to what it should be, you will encounter another problem, that is the console.log not printing correctly. Remember I said this:

When you see repetitive sentences, what code can we use, look at how we print all the places in the array previously?

let vacationSpots = ['Kuala Lumpur', 'London' , 'Spain']

for (let vacationSpotIndex = 0; vacationSpotIndex < vacationSpots.length; vacationSpotIndex++) {
     console.log(vacationSpots[vacationSpotIndex]);
}

The answer is near. How to make it prints longer sentences with the places names? We just add some string interpolation, something like:

console.log('I like to eat ' + [.... variable name... ] );


#10

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