7. Search for a friend


#1

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#
Hello,
My code shows in console 3x times the whole object named friends. I cannot figure out why? PLEASE HELP :slight_smile:

```

var friends = {};
friends.bill = {};
friends.bill.firstName = ‘Bill’,
friends.bill.lastName = ‘One’,
friends.bill.number = ‘111’,
friends.bill.address = [‘One Street’,‘One Town’,1 ],
friends.steve = {};
friends.steve.firstName = ‘steve’,
friends.steve.lastName = ‘Two’,
friends.steve.number = ‘222’,
friends.steve.address = [‘Two Street’,‘Two Town’,2];
var list = function() {
for ( var key in friends ) {
console.log(key);
}
};
var search = function(name) {
for (var x in friends) {
if ( friends[x].firstName === name );
console.log(friends[x]);
}
};
search(‘steve’);

Regards AriKCZ

#2

you have exactly the same problem as in this topic:

Please read it


#3

++++ why second display of last Item
+++++++++++++++++++++++++++++
It is the Javascript interpreter which is running in this course-environment,
it display’s the last non-displayed action…

You will also encounter this, when you are doing a return-statement
as last action…
You could counter this by using a
console.log( " " ); as last statement…

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 ==” );

AND

maybe it is better to change friends.steve.firstName to “Steve”


#4

Hello again.
So i changed my code to be like this:

var friends = {};
    friends.bill = {};
    friends.bill.firstName = 'Bill',
    friends.bill.lastName = 'One',
    friends.bill.number = '111',
    friends.bill.address = ['One Street','One Town',123456 ],
    friends.steve = {};
    friends.steve.firstName = 'Steve',
    friends.steve.lastName = 'Two',
    friends.steve.number = '222',
    friends.steve.address = ['Two Street','Two Town',213456];
/*var list = function() {
    for ( var key in friends ) {
        console.log(key);
    }
};*/
var search = function(name) {
    for (var x in friends) {
        if ( friends[x].firstName === name );
        console.log("first console log");
        
        }
        };
search("Steve");
console.log("end of my script")

And this is what my console shows now
image

My search function still doesnt work as it should. I know i changed it to show “first console log” but it still shows 2x instead of one time.
So what am I doing wrong?

Regards AriKCZ


#5

did you read the topic i linked to? Your if condition still doesn’t have curly brackets ({}) and a unnecessary semi-colon


#6

As first I missread your first topic, so checked it and it took a while since I figured out what was wrong (what a dumb mistake xD). Now it works thank you :slight_smile:

But I cannot bypass the exercise.

var friends = {};
    friends.bill = {};
    friends.bill.firstName = 'Bill',
    friends.bill.lastName = 'One',
    friends.bill.number = '111',
    friends.bill.address = ['One Street','One Town',123456 ],
    friends.steve = {};
    friends.steve.firstName = 'Steve',
    friends.steve.lastName = 'Two',
    friends.steve.number = '222',
    friends.steve.address = ['Two Street','Two Town',213456];
var list = function() {
    for ( var key in friends ) {
        console.log(key);
    }
};
var search = function(name) {
    for (var x in friends) {
        if ( friends[x].firstName === name ) {
        console.log(friends[x]);
        }
        }
        };
search("Steve");
console.log( " " );
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

But I cannot bypass this exercise. It still says “Oops, try again. It looks like your search function doesn’t return contact information for Steve.”

Even I passed it in console:
image


#7