7. Search for a Friend Help Please!


#1

Please help, I keep getting the error message that it does not return search results for Steve but I can't figure out what I have wrong.

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
friends.kelsey = new Object();
friends.alec = new Object();
friends.matt = new Object();

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
}
};
var friends = {
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-4444",
address: ['One Apple Way', 'Hollywood', 'CA', '90210']
}
};
var friends = {
Kelsey: {
firstName: "Kelsey",
lastName: "Sozanski",
number: "(412) 123-4567",
address: ['987 Sesame St', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Alec: {
firstName: "Alec",
lastName: "Evachek",
number: "(412) 555-5555",
address: ['152 Riverview Ave', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Matt: {
firstName: "Matt",
lastName: "Nelson",
number: "(412) 555-4444",
address: ['123 North Side Ave', 'Pittsburgh', 'PA', '15213']
}

};

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

var search = function(name) {
for (var key in friends) {
if(friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
};
list(friends);
search("Steve");


#2

@pens7187,
In your original-Post
you are over-writing the friends object

The propertie are comma-, separated
Use the following definition

var friends = new Object(); 

friends.bill = new Object();
friends.steve = new Object();
friends.kelsey = new Object();
friends.alec = new Object();
friends.matt = new Object();

var friends = { 
 bill: {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
 },
 steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(206) 555-4444",
    address: ['One Apple Way', 'Hollywood', 'CA', '90210']
 },
 kelsey: {
    firstName: "Kelsey",
    lastName: "Sozanski",
    number: "(412) 123-4567",
    address: ['987 Sesame St', 'Pittsburgh', 'PA', '15202']
 },
 alec: {
    firstName: "Alec",
    lastName: "Evachek",
    number: "(412) 555-5555",
    address: ['152 Riverview Ave', 'Pittsburgh', 'PA', '15202']
 },
 matt: {
    firstName: "Matt",
    lastName: "Nelson",
    number: "(412) 555-4444",
    address: ['123 North Side Ave', 'Pittsburgh', 'PA', '15213']
 }

};

#3

what I have wrong?let me kw?
var friends={};
friends.bill={
firstName:"bill",
lastName:"v",
contact:"255525",
address:['fisth','king','sds','22']
};
friends.steve={
firstName:"steve",
lastName:"sds",
contact:"5514",
address:['second','queen','ds','23']
};
var list = function(friends){
for(var friend in friends){
console.log(friend);
}
}
list(friends);
var search = function(name) {
for (var key in friends) {
if(friends[key].firstName === name) {
console.log(friends[key].contact);
return friends[key].contact;
}
}
};
search("steve");


#4

@aaja,

The course expects you to use a capitalized firstName "Bill" and "Steve"

What message do you get....??


#5

@leonhard.wettengmx.n it does not return search results for Steve


#6

@aaja,
Provide us with the FULL code as you are using currently...


#7

var friends = {
bill : {
firstName : "bill",
lastName: "gates",
number : "111 222 333",
address : ["One Microsoft Way", "Redmond","WA", "98052"]
},
// Justo aqui arriba es la coma para separarlos.
steve : {
firstName : "steve",
lastName : "jobs",
number : "444 555 666",
address : ["1 Infinite Loop", "Cupertino", "CA","95014"]
}
};
var list = function(friends) {
for (firstName in friends) {
console.log(firstName);
}
};

var search = function (steve) {
for (var steve in friends) {
if (friends[steve].firstName === "steve") {
console.log(friends[steve] );
return friends[steve];
} else {
console.log (" entry not found");
}
}
};
search(steve);

entry not found
{ firstName: 'steve',
lastName: 'jobs',
number: '444 555 666',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
entry not found
{ firstName: 'steve',
lastName: 'jobs',
number: '444 555 666',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
entry not found
{ firstName: 'steve',
lastName: 'jobs',
number: '444 555 666',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
ReferenceError: steve is not defined


#8

@aaja,

The course expects you to use a capitalized firstName "Bill" and "Steve"