Please help, i have no idea what to do! #12


#1

ok i have no idea what im doing in this one. i just dont know where to even start. please help!

code :

var myArray = [1, false,'paint', 'cat'];

var cat = {
type: 'goofy',
color: 'black',
};
myArray = [cat];

Create an array, myArray. Its first element should be a number, its second should be a boolean, its third should be a string, and its fourth should be... an object! You can add as many elements of any type as you like after these first four.

i really dont know what im doing and i need help!


#2

OK, now your beginning is not bad, so I guess you are just confused for no apparent reason. You just need to read instructions more carefully.

At first it asked you to create an array called myArray then to insert some items in it.
First item should be a number, second - boolean, third - string, fourth - object.

For three items you did everything correct, but look at the fourth element it's also a string, just like third element. But it asks you to insert object as fourth element and not a string!

So, instead of 'cat' you should add {} these brackets are nothing but an empty object literal.
I believe you somehow got confused, because if you're beginner to insert object as an array item can look and feel weird, but really you can add any type of value. so your first line of code should look like:

var myArray = [1, false,'paint', {}];

Instructions don't ask you to define object and add some properties, so I guess you don't need it.
Just remove cat object literal that you have created and that last line of code where you assign [cat] to myArray..

If you need to add a new array item, you have to specify position/index of that array and then assign a value to that index or you can use push method on array in which you want to push a value at the end of an array. In your last line of code: myArray = [cat]; you assign a variable cat wrapped inside [] square brackets, so you basically assigned object literal wrapped inside an array because that's how arrays are defined in JavaScript.

If you want to add an additional item you don't simply assign new value to an array because in that case you're literally destroying all the values that that array holds and after assigning a new value you have only one item in an array, so make sure you're inserting/adding array item and not assigning a whole new value.


#3

var myArray = [1, false,'paint', {}];

var cat = {
type: 'goofy',
color: 'black',
};

so like that? code went through but nothing showed up in the console

#4

If it works then it's ok, but I don't know what further instructions are. Not every exercise will show you something in the console.


#5

Create an array, myArray. Its first element should be a number, its second should be a boolean, its third should be a string, and its fourth should be... an object! You can add as many elements of any type as you like after these first four.


#6

That's all you need I guess then.


#7

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