7. Search for a friend


#1


7. Search for a friend


Frankly, functions confuse me. Would anybody be able to "translate" the code of part 7 like how this guy did for an earlier part? https://discuss.codecademy.com/t/4-iterating-over-an-array/54121/8

Thanks!


Replace this line with your code.


#2

I sensed a disturbance in the force and came to check it out.

Post the code and I'll try my best to 'translate' your code like I did for that part :stuck_out_tongue:


#3

This is the answer they show in step 1:

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']
};

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

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

list(friends);
search("Steve");

I've been trying to do the coding in the other version, here's what I've got that was "Approved" at least as far as step 6:

var friends = Object();

friends.bill = Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = 555-5555;
friends.bill.address = ['one Microsoft Way','Redmond','WA','98052'];

friends.steve = Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = 555-5555;
friends.steve.address = ['1 Infinite Loop','Cupertino','CA','95014'];

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

Thanks so much @jibblyj!
type or paste code here


#4

Any particular part you want me to clarify or do you want me to try the whole thing?


#5

I mostly get lost in the function parts, so that'd be best, thanks!


#6

var list = function (friends) { // 'list' is a function that takes a single parameter - 'friends'
 for (var key in friends) { // For each friend in 'friends'
    console.log(key); // Log that friend's info
 }
}

var list = function(obj) { // 'list' is a function that takes a single parameter - 'obj'
  for(var prop in obj) { // For each friend in friends
    console.log(prop); // Log friend's info 
  }
};

var search = function(name) { // 'search' is a function that takes a single parameter - 'name'
  for(var prop in friends) { // For prop(erty) in friends (for each friend)
    if(friends[prop].firstName === name) { // If [friend named above]'s firstName is the same as the name you gave e.g. search("Bill")
      console.log(friends[prop]); // Log all info on that friend
      return friends[prop]; // Returns friend's info
    }
  }
};

I'm not 100% sure, sorry :confused: It's been a while since I did this...


#7

var search = function(name) { // 'search' is a function that takes a single parameter - 'name'
  for(var prop in friends) { // For prop(erty) in friends (for each friend)
    if(friends[prop].firstName === name) { // If [friend named above]'s firstName is the same as the name you gave e.g. search("Bill")
      console.log(friends[prop]); // Log all info on that friend
      return friends[prop]; // Returns friend's info

I still have some (possibly already explained, I just don't get it) questions:
1. how does the program know that "Bill" or "Steve" is a "name"? It's only described as "firstName" or "lastName" ever in the code.
2. could [prop] be replaced by [key]?
3. How does the code connect [prop], and name together? .bill and .steve are objects, after all, not a property, right? Or are the 2 interchangeable (question 2). Because I think it's ordered like object -> key -> property, and .bill and .steve are in the "key" section?

Thanks!


#8
  1. It doesn't. It checks whether the firstName of each person is equal to the name you inputted.

  2. As long as you replace the word in all instances, then it can be replaced with any unique word (not already used).

  3. .bill and .steve are objects inside other objects, so they are both. They are keys or properties (same thing, different name) of 'friends', but are objects because they contain other things.

:slight_smile:


#9

Object friends in this code is global, function list can see the object friends, why the call of function list is passing object friends as an argument?


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.