Unexpected Identifier what's wrong? lesson 14 javascript


#1


https://www.codecademy.com/courses/javascript-beginner-en-9Sgpi/3/1?curriculum_id=506324b3a7dffd00020bf661


Oops, try again. There was a problem with your syntax.
SyntaxError: Unexpected identifier


var myArray = [21, bool1, "Yo", myObj];


var bool1 = true;

myArray = [bool1];

var myObj = {    
    myObj: "Help!"   
    myObj: "Me!"
}
myArray = [myObj];

what's wrong here?


#5

You should declare myArray after you declare all the other variables. The computer reads the code top to bottom, so it doesn't know what to do when it sees bool1 and myObj because they haven't been declared yet.


#6

could I see an example of code for that please @goredbirds ?


#7

Just move this line:

var myArray = [21, bool1, "yo", myObj];

to the end of the script.
The reason for this is that you need to declare a variable with var before you can use it anywhere. If you use a variable without declaring it first, the computer doesn't work.


#8

ok I tried that and I got the same problem.


#9

So here's the code

var bool1 = true;
myArray = [bool1];
var myObj = {
myObj: "Help!"
myObj: "Me!"
};
myArray = [myObj];
var myArray = [21, bool1, "Yo", myObj];

but It still doesn't work


#10

Wait sorry I see another error.
First of all, you shouldn't have the same key twice in an object. Also, you need a comma after the first item

var myObj = {
myObj: "Help!"//needs comma at the end
myObj:"Me!"//can't use the same name twice

#11

so change the second item to myObj1 or something like that and you should be good


#12

thanks for the help but now I need help on an "ultimate kicker"
adding an object to a 2D array


#13

here's the code and the messages I'm getting

var myObj = {
myObj: "Happy",
myObj1:"sad"
};
newArray = [MyObj];

var newArray = [ [1, 4, myObj], [7] ];

Oops, try again. Your code doesn't look quite right. Feel free to peek back at earlier exercises if you need a syntax refresher! And check out the error message for more details: ReferenceError: MyObj is not defined

ReferenceError: MyObj is not defined


#14