Question regarding the contact list output- eager to know why~~


#1

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#

I did as the instructions said. However I don't understand why the output keeps repeating and besides when I
use search('Steve');, it not only output the result of 'Steve' but also ''Bill'.
I have tried to delete the code console.log(friends[key]); , which is what the instruction told us to do, then the output only log "Steve"'s information once and this seems the right output for me.
I wonder if it is necessary to add console.log(friends[key]); in the search function? Also, why does this code keeps repeating my contact info and log other's contact information which I didn't search in the meantime?


var friends={};
friends.bill={
    firstName:"Bill",
    lastName:"Lala",
    number:04882739,
    address:["Princess st","14","Kapoa","Queesland","4387"]
    };
    
friends.steve={
    firstName:"Steve",
    lastName:"Lala",
    number:93858390,
    address:["Princess st","10","Kapoa","Queesland","4387"]
    };
var list = function(friends){
    for (var key in friends){
    console.log(key);
    }
   };
    console.log("");
    
var search = function(name){
    for(var key in friends){
    if(friends[key].firstName===name){
    console.log(friends[key]);
    return friends[key];
   
    }
    }
    
    };
    
    
    search("Steve");


#2

the exercise runs validation, this can cause output. To see what you output really looks like you can run your code here


#3

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