Search for a friend 7/8


#1


exercise


Hello everyone,

I am a beginning programmer and I just had my first year of coding lessons, but I still have some issues with JavaScript. That's why I decided to start this course on Code Academy.

(sorry for my somewhat bad English, but English is not my primary language, therefore it may contain some errors but I hope you'll understand what I have to say)

I was stuck at this exercise for a pretty long time, and after some reviewing I finally got the message that my code was correct. I was happy!

But when I checked out what was logged, each contact was logged four times. And after these 4 logs, the same contacts were logged another time, but now horizontaly. I checked my code over and over again, but I can't seem to find the error.

Can anyone help me please, your help would be greatly appreciated!


var friends = new Object();

friends.bill = new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="22";
friends.bill.address=["Maalbeekstraat", "Dilbeek", "Belgium", "1700"];


friends.steve = new Object();
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number="24";
friends.steve.address=["Maalbeekstraat", "Dilbeek", "Belgium", "1700"];
 
 
var list = function (contacts){
    for (var friend in contacts){
        console.log(friend);
    }
};
list(friends);

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

search(friends);


#2

Codecademy doesn't redirect output when it's calling your code, and it also prints the result of the last expression, so some of that extra output could be from that. You can tell which that would be by adding a print statement at the very end of your code, anything that is printed out after that print will be caused by something other than your code.

If you're still getting duplicate prints above that print, then you'll just have to check all the places where you print something and consider how many times it executes/where that code is called.

For example your search function is both printing and returning, that is rather strange behaviour. Perhaps it's just meant to return.


#3

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