4. Adding Properties


#1


Hi, been stuck on this for a while.... I'm not sure whats wrong with it....

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/4?curriculum_id=506324b3a7dffd00020bf661#


var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
var friends = {
    bill: {
        firstName: "Bill"
        lastName: "Someone"
        number: "(459)867838"
    }    
    steve: {
        firstName: "Steve"
        lastName: "Someone"
        number: "(451)789234"
    }
};


I would appreciate any help :slight_smile:


#2

Ok first of all decide on whether to use literal or constructor notation because currently you create a friends object with two object properties bill and steve here:

var friends = new Object();
friends.bill = new Object();
friends.steve = new Object();

that are immediately thrown away to create a completely new object:

var friends = {
    bill: {
        firstName: "Bill"
        lastName: "Someone"
        number: "(459)867838"
    }    
    steve: {
        firstName: "Steve"
        lastName: "Someone"
        number: "(451)789234"
    }
};

you even used var again although there was already a variable friends declared.
So the first 3 lines are essentially pointless. Now the syntax errors occur because you do not use , to separate your properties:

    var object = {
         innerOBJ1:{
             key1:value1,
             key2:value2,
             finalKey:finalValue // no comma here
         }, // because you have a new property comming
         innerOBJ2:{
             key1:value1,
             key2:value2,
             finalKey:finalValue // no comma here
         } // last property therefore no comma here
    };

#3

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