7. search for a friend: output seems odd


#1


12.Contact List -> 7. Search for a friend


code allegedly works fine, however the output shows Steve's contact info TWICE and then it adds Bill's info too, i don't know what causes this, i have had a lot of trouble figuring out the code so excuse me for any dumb mistakes that might be the case.


var friends = {
    bill: {
        firstName : 'Bill',
        lastName: "McBillson",
        number: 5555213124,
        address: ['Mcbillson st.']
        },
    steve: {
        firstName: 'Steve',
        lastName: 'Jobs',
        number:1223214211,
        address: ['no jobs rd.']
        }
}
var list = function(friends){
    for(var key in friends)
    console.log (key)
}

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

    //this is the output:
 firstName: 'Steve',
  lastName: 'Jobs',
  number: 1223214211,
  address: [ 'no jobs rd.' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: 1223214211,
  address: [ 'no jobs rd.' ] }
{ firstName: 'Bill',
  lastName: 'McBillson',
  number: 5555213124,
  address: [ 'Mcbillson st.' ] }


#2

The SCT tests our code and since the search function logs output, we see the output from those tests.

The for loop in your list() function is missing the code block { }.


#3

Sorry, I'm still confused ... what code block { } is the code missing, when it produces the duplicate of the "Steve" output? Like, what should the function look like, to avoid that?


#4

    for (var key in friends) {

    }

We cannot avoid the extra output since it is out of our hands. We do not cause it, the SCT does. Just ignore and continue on.


#6