Return a Contacts infomation help please!


#1

Hi, I am having a similar issue as some other coders where it will return the same address a lot of time instead of just once but mine does NOT pass, here is my code:

var list= function(friends) {
for(var firstName in friends) {

console.log(firstName);

}
};

var search= function(name) {
for( var key in friends) {
if(friends[key].firstName ===name) {
console.log(friends[key]);
return friends[key];
} else {
return ("I'm sorry this name does not exist");
}
}
};

search('Bill');

var friends= {
Bill: {
firstName: 'Bill',
lastName: 'Builder',
number: 07762678364,
address: ['7 moorlands RD', 'Camberley', 'Surrey']
},
Steve: {
firstName: 'Steve',
lastName: 'Wonder',
number: 07762678359,
address: ['Bombay Beach', 'Sydney', 'NSW']
},
Cristhian: {
firstName: 'Cristhian',
lastName: 'Gambobo',
number: 0127661946,
address: ['Wall St', 'TriBeCa', 'NY']
}
};

I call 'Bill' and i get the contact information for Bill repeated three times:

{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
{"firstName":"Bill","lastName":"Builder","number":7762678364,"address":["7 moorlands RD","Camberley","Surrey"]}

However when i call 'Steve' it doesn't work at all, it just gives me Bills contact information again and it also returns my else {} response:

{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
"I'm sorry this name does not exist"


#2

@mafecardozo7,

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

Thus use

search('Bill');
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

#3

Hi, thanks for the reply, but it's still not working, what was this function supposed to do? Any other suggestions?

Many Thanks


#4

i have just removed the else {} and it has now passed, but I really don't see how that could effect my code and why it wasn't allowing me to pass?


#5

@mafecardozo7,
The course expected the data set to look like

var friends= {
bill: {
firstName: 'Bill',
lastName: 'Builder',
number: '07762678364',
address: ['7 moorlands RD', 'Camberley', 'Surrey']
},
steve: {
firstName: 'Steve',
lastName: 'Wonder',
number: '07762678359',
address: ['Bombay Beach', 'Sydney', 'NSW']
},
cristhian: {
firstName: 'Cristhian',
lastName: 'Gambobo',
number: '0127661946',
address: ['Wall St', 'TriBeCa', 'NY']
}
};

And concerning the return statement
which can only be used in a function

If this return statement is executed
the function is exited !!!

Reference::

google search
=the book=
javascript variable site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
javascript return statement site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
=discussions=
javascript return statement site:stackoverflow.com
http://stackoverflow.com/questions/3717420/is-there-a-benefit-to-using-a-return-statement-that-returns-nothing
http://stackoverflow.com/questions/22134753/return-statement-in-javascript