Ex 7 . Code working but printout very weird, why?


#1

My code works and gives no error messages but prints out Steve three times before printing out Bill once and then giving the Steve contact details as below.

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(309) 555-5555',
address: [ 'Two Apple Way', 'ST Margarets', 'London', 'TW10 1TY' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(309) 555-5555',
address: [ 'Two Apple Way', 'ST Margarets', 'London', 'TW10 1TY' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(309) 555-5555',
address: [ 'Two Apple Way', 'ST Margarets', 'London', 'TW10 1TY' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '(206) 555-5555',
address: [ 'One Microsoft Way', 'Richmond', 'London', 'TW9 4DR' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(309) 555-5555","address":["Two Apple Way","ST Margarets","London","TW10 1TY"]}

Does anyone Know why it does this. Below is the code I have used.

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way','Queens','New York','ZY1 4AB']
},
friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(309) 555-5555",
    address: ['Two Apple Way','ST Margarets','London','TW10 1TY']
};

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

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

thankyou :slight_smile:


#2

The additional output is the result of the lesson checker searching for "Bill" and "Steve". Since the function logs output, we see the extra output from this test. Ignore it.


#3

Thank-you that makes sense.
Especially as the program magically started working later on even though no changes had been made


#4

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