I don't know where to declare my variables


#1

Hello,

As I’ve been practicing JS in web consoles, I’ve had an error which I am
really have no idea how to fix.

You see, I’m trying to run a small code with a for loop. Apparently the loop is correct, but I’m
defining the variables in the wrong place. Here’s the code (note: the code is just for the sake of practice):

var nuclearCountries = [
'North Korea (DPRK)’,
northAmericanCentralCountry,
someAsian
];

var northAmericanCentralCountry = ‘United States of America’;
var someAsian = ‘India’;

for (var i = 0; i <= nuclearCountries.length; i++) {
console.log(nuclearCountries[i]);
}


<span style="color:grey">*I've posted another something similar in the JS forums before, but I've fixed the main problem of that case and now I'm here looking to solve the rest of the error.</span>

If you are willing to reply, it would be appreciated if you could give a visual example along with your reply.

Thank you


#2

here:

'North Korea (DPRK)’,

what you use to close the string isn’t an apostrophe.

well, you have to define variables before you can use them. so you have to define northAmericanCentralCountry and someAsian variable before the nuclearCountries array


#3

stetim, does this code look any better?

var northAmericanCentralCountry = ‘United States of America’;
var someAsian = ‘India’;

var nuclearCountries = [
'North Korea (DPRK)',
northAmericanCentralCountry,
someAsian
];

for (var i = 0; i <= nuclearCountries.length; i++) {
console.log(nuclearCountries[i]);
}

Because when I press enter, I get this error message:

VM1580:3 Uncaught SyntaxError: Unexpected token ILLEGAL
    at Object.InjectedScript._evaluateOn (<anonymous>:878:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:811:34)
    at Object.InjectedScript.evaluate (<anonymous>:667:21)

#4

yep, but the string on this line:

var northAmericanCentralCountry = ‘United States of America’;

isn’t properly enclosed, those are not apostrophe/single quotes you use

also, check your loop condition.


#5

What do I enclose it with then?


#6

single quotes or double quotes


#7

Would an example be possible?
Thanks


#8

you use single quotes/apostrophes here:

'North Korea (DPRK)',

so i don’t see the problem. So you already have an example :wink:


#9

Thankyou stetim, without your help I would still have no idea.:wink:


#10

if you don’t know how to make a string, it would be wise to do a python course somewhere.

yes, you fixed it now, but this is so fundamental.


#11

Then can you tell me what’s wrong with this?

var whereDoKoysLive = [‘Hokkaido’, ‘Honshu’, ‘Shikoku’] ;

for (var i = 4; i < whereDoKoysLive.length ; i–) {
console.log(whereDoKoysLive[i]);
}


#12

arrays are zero indexed based, so a list with 3 items has 2 as highest index, so not sure why you would do i=4

furthermore, are you going to loop from high to low (right to left) or low to high (left to right)?

for right to left, change the loop condition to make it stop at zero.


#13

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