7. Search for a friend. Help


#1

When I use the following code I get the error message: "SyntaxError: missing : after property id". I've searched the forums already and most of the example code looks very similar, I'm not sure what I am missing.

var friends = {

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "404 562-2755",
address: ['One Fake Street','Inglewood', 'CA', '40436']
},
friends.steve= {
firstName: "Steve",
lastName: "Jobs",
number: "230 515-7721",
address: ['Two Diamond Road', 'Richmond', 'VA', '12723']
}
};

var list = function(friends) {
for (var i in friends) {
console.log(i);
}
}

var search = function(name) {
for (var i in friends) {
if(friends[i].firstName === Bill) {
console.log(friends[i]);
return friends[i];

    }
}

};


#2

Use : not = for the properties of your friend object


#3

I don't understand. Do you mean use 'friends.steve: {' instead of 'friends.steve = { '?


#4

Closer, but not quite. Compare to how you are adding properties in the other objects literals.


#5

I'm looking at the What you'll be building page and it is written how I had it originally. I cleared it up to make sure I was using the same notation style consistently. I can't see any difference from the sample page apart from not calling the list or function.

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "404 562-2755",
address: ['One Fake Street','Inglewood', 'CA', '40436']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "230 515-7721",
address: ['Two Diamond Road', 'Richmond', 'VA', '12723']
};

var list = function(friends) {
for (var prop in friends) {
console.log(prop);
}
}

var search = function(name) {
for (var prop in friends) {
if(friends[prop].firstName === Bill) {
console.log(friends[prop]);
return friends[prop];

    }
}

};


#6

That's another way to do it. But you could just the same have written the whole thing as a string literal, which is what you are already doing for bill and steve, you would just have to add bill and steve as properties to friends.


#7

I'm still lost. This is how it looks on the "What you'll be building" screen.

var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']

This is what I have written.

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "404 562-2755",
address: ['One Fake Street','Inglewood', 'CA', '40436']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "230 515-7721",
address: ['Two Diamond Road', 'Richmond', 'VA', '12723']
};

I don't see the difference.


#8

You can do this:

bill = {};
bill.firstName = 'Bob';

Or..

bill = {
     firstName: 'Bob'
}

They do the same thing. You were doing something inbetween, which doesn't make sense to the interpreter.

You were doing:

bill = {
    bill.firstName = 'Bob'
}

#9

Why would it run successfully on the sample page? It still comes back with "missing before statement".


#10

Two of the examples in my previous post are valid. One is not.


#11

I went back and retook the lesson and found out where it was going wrong. Taking some of the Objects1 unit was very helpful. I saw what I was doing wrong. Sorry for all the hassle.


#12

I solve this problem.
Try to change friends.steve to friends.Steve.
Bill need ,too.
: )


#13

var friends={
bill:{},
steve:{}
};
friends.bill={
firstName:"Bill",
lastName:"Gates",
number:"(206)555-55555",
address:["LOL",20]
};
friends.steve={
firstName:"Steve",
lastName:"Jobs",
number:"(206)444-44444",
address:["TROL",21]
};
var list = function(friends){
for(var bill in friends){
console.log(friends[bill]);
}
}
list();
var search=function(name){
for(var key in friends){
if(friends[key]===name)
{
console.log(friends[key]);
}
}
return friends[key];
}
var me=search("Steve");
console.log(me);

Try this out