Question 7 Search


#1

So I have entered code that does allow me to pass this question but the stuff that comes up in the console window does not look right! Below is my code, I have removed the list function for simplicity:-

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

friends.steve = {
firstName: "Steve",
lastName: "Stanley",
number: "123",
address: ['17 Maungawhau Rd', 'Newmarket', 'Auckland']
};

friends.bill = {
firstName: "Bill",
lastName: "Bob",
number: "234",
address: ['20 D Crockerton', 'Tooting Bec']
};

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

search("Steve");

AND HERE IS WHAT IS LOGGED IN THE CONSOLE, I CAN'T UNDERSTAND THE REPITITON, WHY "NO MATCH" COMES UP OR WHY BILL'S INFORMATION SHOWS. ANY CLARIFICATION MUCH APPRECIATED!

No Match
{ firstName: 'Steve',
lastName: 'Stanley',
number: '123',
address: [ '17 Maungawhau Rd', 'Newmarket', 'Auckland' ] }
No Match
{ firstName: 'Steve',
lastName: 'Stanley',
number: '123',
address: [ '17 Maungawhau Rd', 'Newmarket', 'Auckland' ] }
No Match
{ firstName: 'Steve',
lastName: 'Stanley',
number: '123',
address: [ '17 Maungawhau Rd', 'Newmarket', 'Auckland' ] }
{ firstName: 'Bill',
lastName: 'Bob',
number: '234',
address: [ '20 D Crockerton', 'Tooting Bec' ] }
{"firstName":"Steve","lastName":"Stanley","number":"123","address":["17 Maungawhau Rd","Newmarket","Auckland"]}


#2

@supasuma,

-1

Remove the ELSE

else {
 console.log("No Match");
 }

##-2
After your code-line

search("Steve");

add the code-line

console.log( "==End of my code==");

and you will see that some extra output is produced by the code-checker.....


#3

Thanks Leon(?) that was really helpful!


#4

Thank you SO much for that second tip. Do you know why on Earth the code-checker would be spitting out that extra stuff on the console? It has been driving me crazy all day until you pointed it out...