7/8 - Doesn't accept the right answer!


#1

My code seems to be fine and it also gives out the correct output.

var friends = {} ;
friends.bill = {
firstName: 'Bill' ,
lastName : 'Gates' ,
number : '(340) 452-345)' ,
address : ['JPNGR','7th cross','awesome']
};
friends.steve = {
firstName : ' Steve' ,
lastName : 'Jobs' ,
number : '(534) 564-353 ',
address : ['Jaynagar','21st cross','cool']
} ;
var list = function(friends){
for(var i in friends){
console.log(i) };
} ;
var search = function(name) {
for(var key in friends) {
if(friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
};


#2

When you defining an object its suppose to follow this format:

var objectname{
propertyname: propertydefintion
}

Not like this:

So when you change that to the correct syntax:

var friends = {
bill: {
firstName: "Bill",
lastName: "Thorton",
number: "(765) 869-7654",
address: ['42 East Redwood St', 'Charming','CA','99017']
},
steve: {
firstName: "Steve",
lastName: "Johnson",
number: "(765) 246-8435",
address: ['64 Clarence St','Charming','CA','99017']
}
};

Voila! :smile: Hope this helped!


Intrested in Web Develpoment? Want to know more? Where to go next?This is the place for you!

Instrested in Game Development?Want to know more? Where to go next? This is the place for you!


#3

This is a little misleading as this is not a constructor, you're defining/declaring the object. I tend to notice a difference in terminology when coding, that defining/declaring is reserved for the instance of a literal creating of a unique name value (identifier) and it's value, such as [var variablename = value;]. Calling an identifier (functions too) is when you're requesting that value from the identifer, whatever it may be, into a section of the script. A constructor allows you to define the structure of an object, and using the new Object() it applies those properties with an empty object name to a new identifier.

From: http://www.w3schools.com/js/js_object_definition.asp
There are different ways to create new objects:

Define and create a single object, using an object literal.
Define and create a single object, with the keyword new.
Define an object constructor, and then create objects of the constructed type.

Not saying it's wrong, just misleading, which could be confusing down the road. Here are some of my personal notes about objects and constructors. @anriod, use these if you'd like along with @amanuel2 's more detailed example of how your code should look.

//Another way we can declare Objects is by defining our own Custom Constructors. These constructors act like functions, and allow us to apply properties to the Object when it is created. Applying properites upon the creation of an object is also known as literal notation.
function Object(parameter1, parameter2) {
    this.property1 = parameter1;
    this.property2 = parameter2;
}

//This method of declaring an Object is known as the Object Constructor, by using the "new" constructor and Object() statement, a blank Object is created with no properties, unless parameters are passed through. The variable name will then become the identifier for the blank object class.
var empty = new Object('Charley', 5);
empty.property1 //This property will now equal the first parameter which was the string "Charley."
empty.property2 //This property will now equal the second parameter which was the number 5.

Also @amanuel2, I seem to be getting all of your notifications since you've been linking those topics haha! I guess because i have a comment in one of them?


#4

@amanuel2 @emgo_dev but my question - why is it not accepting this?(No syntax errors - it says " It looks like your search function doesn't return contact information for Steve." . ??


#5

Because it dosent give you the same result in the window.......

Correct one:

Your way:


#6

@amanuel2 Nope my way is correct! The problem was instead of -
firstName : 'Steve'
It was -
firstName : ' Steve'
That space made all the difference.


#7

I mean, im just saying the way i listed is a good practice for your future in coding. Thats probably a bug in codecademy but when you are outside of codecademy practices like that wont work :wink:


#8

Again I disagree because what I'm basically doing is -
1.Creating a empty Object
2.Later adding elements to that Object
It's perfectly fine try it in your browser console if you want. :grinning:


#9

Well if it works in your browser then it is completely legitimate :smile:


#10

Thank you! This worked!!