7.Search for a friend


#1

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['Miracle Mile', 'LA', 'California']
},

steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(206) 666-5555",
    address: ['Palo Alto', 'San Francisco', 'California']
}

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

The error I get is that first, it says "Key is not defined", and second it just logs the entire code to the console

Thanks for your help!


#2

you need to change your var list = function (friends)

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

Then rewrite your search to look something like this:

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

Then list your friends

list(friends);

and then searh for steve

search("Steve")


#3

i did it like this, and it works:

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

};

good luck :slightly_smiling:


#4

Thank you! It worked


#5

Thank you for you help! :slight_smile:


#6

My code was
var friends = {
bill: {
firstName: "Bill",
lastName: "Fence",
number: "(319)-456-4566",
address: ['Birch Dr', 'Iowa City', 'IA', '50264']
},
steve : {
firstName: "Steve",
lastName: "Eng",
number: "(319)-465-4655",
address: ['Oak Dr', 'Iowa City', 'IA', '50264']
}
};
var list = function(friends) {
for (var friend in friends) {
console.log(friend);
}
};
var search = function(names) {
for (var friend in friends) {
if (friends[firstName].firstName === name) {
console.log(friends[lastName]);
return friends[number];
}
}
};
search("Bill");
and it says
did you create a function called search?
and YES, i did create a function called search. it makes no sense. :confused:


#7

@herardojones

Replace firstName in the brackets by friend. No such name exist replace it with names (the optional parameter in your search function).

Should have friend in brackets.


#8

I have come across a little error. In the initial lesson, where the original template is shown, the code when executed prints out a specific defined print out in console. when completing the lesson in the Search for a friend lesson, the same result does not occur and it repeated some of the information in the console.

in order to fix this, which took me a nice good 20-30 minutes i placed the return of the if outside of the if ending curly brace and it worked as it should.

When wondering why this might happen, the fact that the in initial lesson, the "What you will build" lesson has you press the submit and next buttons, without playing with the code, will no matter what bring for the same conclusion and same print out in the console. Either this is a malfunction or i have found something that i shouldn't. *eye brows raised, wondering if this is intended.

it would be nice if it can be explained to me and if i am correct, perhaps fixing this small bug.

thank you and have a wonderful day
12Js.qubexon


#9

`var people = {};
people.bill = {
name: "Bill",
lastName: "Billy",
number: "error",
address: ['One Way','Random']
};
people.steve = {
name: "Steve",
lastName: "Stevy",
number: "unknown",
address: ['Infinite RX','Copenhagen']
};

var list = function(smth) {
for(var wtf in smth) {
console.log(wtf);
}
};

var search = function(name) {
for(var wtf in people) {
if(people[wtf].name === name) {
console.log(people[wtf]);
return people[wtf];
}
}
};

list(people);
search("Steve");`

this is what console wants to see, actually working code, just copy paste from intro exercise and changed names , because after 2 hours writing own code i stuck and got depressed. there is no any comments 'what does what ? 'and no explanation on left side rather then only confusing messages or !!!! signs as if everything goes right if you make smth without understanding for what actually it will be used for.
i would suggest to check exercises again and remove nonsense comments on the left side and put there detailed information.
we are here to learn , like a child who learns language from zero or maybe far experienced.
if there is no right explanation what does what then it is the same like to answer some child who asks why he/she has to take care of her/his teeth and parents just say "just so !, do it" if you do it you are good if not you are bad!"
not the camera takes photos but photograph. please in learning console add comments , even they are too easy for you but not for me or us! . thanks , without offense please i just see you do good job making such a good interface for people but without critique no further Entwicklung!


#10

@objectjumper41919

If you have a problem, please start off a new topic: question + error message + code


#11

thanks, i was a little bit confused that day :), maybe written post post in aggressive way , i will consider your advise next time


#12

What's wrong with this code, for 7? Because, the left, demonstrates nothing, and, I don't understand, a thing it wants me, to do. (The code's a bit, split up.)

var friends = {
bill: {
firstName: "Bill",
lastName: "Cipher",
number: "(217) 819-2728",
address: ['Galaxy St.','Univille','Illuminati, (IL)','1337']

}, 
steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(696) 969-6969",
    address: ['Place St.','Somewhere City','Whatever, (WE)','9001']
}

};

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

list(friends);


#13

i did that and it says the same thing, but the section beside the code showed the information. still confused. :confused:


#14

var search = function (name) {
for (var steve in friends) {
if(friends[steve].firstname === name) {
console.log(friends[steve]);
return friends[steve];
}
};
};

mine says that Steve is not defined