7.search for friend


#1

the code is correct but dont understand the for/in loop and the if condition plz explain thanks .
var list=function() {
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[firstName];
}
}
};
search("Steve");


#2

Is this not an oxymoron? Isn't code built upon our understanding of the problem and our proposed solution toward that end? How does one arrive at solution code that they cannot understand? Did they not just build it?


#3

m trying the different ways to get solution and take help from the Q&A forum thats i done it but not completely understand it.


#4

@msaqibik
Your code is correct
check your friends name should starts with capital letter

like Steve not steve
Bill not bill


#5

I'm in the same spot...code is right but I don't understand it.

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

var search = function(name){
for(var key in friends){
if(name === friends[key].firstName){
console.log(firstName,lastName,number,address);
return firstName,lastName,number,address;
}
else {
return "No such friend.";
}

};

Why does the first function seem to work as well with no parameter as with a false parameter, and why does the second function need a parameter? Why does the first function operate when you plug in the firstName variable as "key" in the "for" loop, while the second one does not? What does the friends[key] thing do if key isn't defined anywhere else?

It just seems like the first function is extremely simple, while the second one seems overly complex.

So confused.


#6

We are given a number of objects, all contained in one object, friends. These are what I call primary objects since they hold the primary keys of the object. Their value may be set to any object, including {}.

Accessing those objects requires an identifierName or some subscript nomenclature that identifies the target object. Eg.

    friends['bill']['firstName']

or,

    friends.bill.firstName

Either way, we are accessing data. Either form may return a reference error, but only the former may be used dynamically (with a variable placeholder in either index).


#7

The reason you need to have a working parameter in the second for/in loop is because you are comparing the data inside the loop to some data that is outside of the function and therefore out of the scope of that for/in loop. You need to pull in data into the function that is otherwise kept separate. The first, more simple, function doesn't need to have a parameter because the function only lists info.

@Reply me if you want further clarification.

<3Jon