Heterogeneous arrays HELP


#1


Heterogeneous arrays exercise


I keep getting the error code " Make sure you have at least four elements in myArray."


`

Replace this line with your code.

var myArray = [12, true, 'Duncan', me];
myArray = [me];
var me = {
name: 'Paige'
};
console.log(myArray);
,,,


#3

This declaration should follow the definition of me. It and the next one reference me before it is defined.


#4

Hi mtf,

Could explain to me how it works? I am so confused.


#5

You have an object with the variable name, me, but it is not defined until the third line. This means it does not exist yet when the first two lines are parsed so those lines will not contain a defined value or object for me.

When you log the array, the output will be,

[ undefined ]

Move the definition for your object to the top of the code so it comes first, followed by the other two lines. Now the output will be,

[ { name: 'Paige' } ]

#6

When i write out my code how come my array has to before the console.log?

Example;

var bool1 = true;
myArray = [me];
var me = {
name: 'Paige'
};
var myArray = [12, bool1, 'Duncan', me];
console.log(myArray);


#7

For the same reason it has to written above where it is first referenced.

// define an object
// access an object

in that order. If we attempt to log an object that is not defined, it could throw an error.

 > console.log(me)
ReferenceError: me is not defined

#8

ok got. Thanks mtf. :slight_smile:


#9

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