Finding it hard to decipher


#1

Hey people! I struggled a bit and finally put together the code following the instructions. My code is not far from correct, but I'm finding it hard to decipher.

  1. I want the code to say "None." when the name entered for search cannot be found in the contact. But my code doesn't work the way I want it to. Why? In the "if" I put console.log cause I think if I put "return" it simply terminates the loop?
  2. In the function "list" why does it not print out the "value", but instead, only the "key"? Is it because the "value" is actually regarded as "the 2nd object"?
  3. Could someone pls tell me what "for (var contact in object)" this loop means exactly?

Thanks!

var friends = {};
  friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "0934874219",
    address: ["3", "Rose Avenue", "Boston", "MA"]
   };
  friends.bill = {
    firstName: "Bill",
    lastName: "Waters",
    number: "3849373911",
    address: ["5", "Venice Beach", "CA"]
  };

var list = function(object) {
    for (var contact in object) {
        console.log(contact);
    }   
}

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

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

#2

but then you should place return "None" after the for loop? Because now, if the first entry is not match, your function will return "None", given a function ends when a return keyword is reached. (if the return keyword is reached in a loop, the loop will need to break in other for a function to end)

But if you found the right person, its okay to make the function end by returning the correct information

2 and 3: the for in loop will loop over all properties in object, and assign them to contact, so contact will contain the properties of friends (which are bill and steve

the confusing thing is that bill and steve (being properties of friends object) are also objects themselves


#3

@stetim94 Thanks man for the tips! I understand now...that's why we need that "return friends[contact]" in the "if", because if the name is found, the. function should stop there, otherwise it will go to "return "None.""

OK got it thanks a lot!


#4

i would personally remove else, and simply place return "None" after the for loop:

for (var contact in friends) {
        if(friends[contact].firstName === name) {    
            console.log(friends[contact]);
            return friends[contact]
       }
}
return "None"

#5

@stetim94 Yup that's what I did. Return directly after the for loop. Without else. Otherwise it just wouldn't run. :slight_smile: Thank you! :smiley:


#6

you might be able to do for else as well:

for (var contact in friends) {
        if(friends[contact].firstName === name) {    
            console.log(friends[contact]);
            return friends[contact]
       }
}
else {
     return "None"
}

but you shouldn't have if/else, because then if the first entry is not a match, else will run, causing the function to end (the loop will break)


#7

@stetim94 Actually I'm not able to. I tried, and it said unexpected keyword "else".


#8

not possible JS, good to know. I seem to mix up Python and JS. Not really doing a good job today :stuck_out_tongue: But you do understand why we moved return None to outside the loop?


#9

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