4. adding properties


#1


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


why is this code not working?


var friends=new Object();
friends.bill=new Object();
    bill.firstName="Bill",
    bill.lastName="walker",
    bill.number="980-241-2222";
friends.steve=new Object();
    steve.firstName="Steve",
    steve.lastName="walker",
    steve.number="980-222-1234";


#2

Try using

in your code..


#3

The Basics:
An object has one or more properties separated by a comma-,
Each property consists of a property-key and it's associated value

var friends ={ name: 'shantanu' , contact:'9557711991' };
The friends object has 2 properties:
1. a name property with property-key name and a string value 'shantanu'
2. a contact property with property-key contact
and a string value '9557711991'

You can either create an object using the literal notation as was done
defining the object friends.

OR

You create an object using the constructor notation.
first you create an empty Object using the following syntax
either with
var friends = new Object();
OR
var friends = {};
THEN
you add the properties by using the following syntax
friends.name = 'shantanu';
friends.contact = '9557711991';
THUS

var friends = {};
friends.name = 'shantanu';
friends.contact = '9557711991' ;
console.log( friends );

will give output:

{ name: 'shantanu', contact: '9557711991' }

You should also familiarize yourself with following syntax
In the constructor notation you can
add a property
EITHER
the object-name dot literal property-key equal-sign associated value
friends.email = "alex@codecademy" ;
OR
the object-name squarebracket-open the property-key as string literal squarebracket-close equal-sign associated value
friends["email"] = "alex@codecademy" ;
OR
using variables

var thePropertyKey = "email";
var theAssociatedValue = "alex@codecademy.com";
friends[thePropertyKey] = theAssociatedValue;
console.log( friends );

will give the output:

{ name: 'shantanu', contact: '9557711991', email: 'alex@codecademy.com' }

#4

thank you that helped alot


#5

what is wrong with my code. Its not passing

var friends ={};
friends.name ="Steve";
friends.address = "washington blvd";
friends.bill ={};
friends.steve ={};
friends.sanjana ={};
friends.bill.firstName = "Bill";
friends.bill.lastName ="Gates";
friends.bill.number = 789654;
friends.bill.address = ["essex street","76", "jersey city","NJ"];
friends.steve.firstName ="Steve";
friends.steve.lastName = "Jobs";
friends.steve.number =987654;
friends.steve.address = ["washington blvd", "4401", "jersey city","NJ"];
friends.sanjana.firstName ="Sanjana";
friends.sanjana.lastName = "Dhanwantri";
friends.sanjana.number =989765;
friends.sanjana.address =["10","regent street"];


#6

What is this for

 friends.name ="Steve";
 friends.address = "washington blvd";

and it might be that the number should be a String-Value


#7

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