JavaScript, Contact List: Search for a friend


#1


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

I didn't get any error messages, however when I use search("Steve") it prints out his information four times, and prints out Bill's information once. I don't understand what I'm doing wrong, any help would be appreciated!

The output for my code:

{ firstName: 'Steve',
lastName: 'Evans',
number: '324-178-8726',
address: [ '327 East Harlem Way', 'New York', 'NY, '10234' ] }
{ firstName: 'Steve',
lastName: 'Evans',
number: '324-178-8726',
address: [ '327 East Harlem Way', 'New York', 'NY, '10234' ] }
{ firstName: 'Steve',
lastName: 'Evans',
number: '324-178-8726',
address: [ '327 East Harlem Way', 'New York', 'NY, '10234' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '353-288-1786',
address: [ '284 West 26th Street', 'New York', '11211' ] }
{"firstName":"Steve","lastName":"Evans","number":"324-
178-8726","address":["327 East Harlem Way", "New York","NY","10234"]}


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

search("Steve");


#2

@afriedman13,

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 to see the result......
use

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

#3

What Leon is getting at is that the windows is actually doing the calls to the function 'search' and 'friends' in the background, that's why the examples are not telling you to make the calls, it's doing it for you. So if you do put in the call "search("Steve");" it is going to show as double the output in the console viewer. I don't know why they do it either, they should just make the student do it and not be a blind call on their part. If they just let the student do it then there would not be the requirement of calling one bill and the other steve.


#4

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