Contact List Returning Multiple Keys From The Two Names Even Though the Program Only Instructs for One


#1

Hi everyone!

I'm here to talk about a problem with the Contact List. The code you see below has an issue where the code returns and logs the keys of both our friends Bill and Steve. I only programmed the function to search for the name in the input, which I set as Steve. I do not get why the console returns Steve three times, then Bill once in the End. I tested if the object literal notation was affecting the results, but it was not. Testing different notations and the constructor both gave similar results. Comparing it to the real and default example in the beginning, it was similar except the example code used the other type of Object Literal. Even when I try entering the example code into the console, the results are the same, both names are still shown. I think the notation or constructor is not to be blamed, but inside the console?

var friends = {
    bill: {
        firstName: 'Bill',
        lastName: 'Gates',
        number: '2132131231',
        address: ['Micro Way', 'Neverda', 'CL', '32465'] 
    },
    steve: {
        firstName: 'Steve',
        lastName: 'Jobs',
        number: '231321321',
        address: ['Legal Rd', 'Nirvania', 'AFBI', '201603']
    }
};   

var list = function(people)
{
    for (var number in people)
    {
        console.log(number);
    }
};

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

list(friends);
search('Steve');

#2

i think your code is right.
run same code here
What you'll be building

@brokenavocado


#3

@brokenavocado, if you run your program here, does the output match your console in the classroom setting?


#4

@msfrisby Yes it does! Thank you, but i'm still wondering why it won't work for the console on the actual lesson...


#5

@brokenavocado Good, that's a good thing! As they don't always seem to match...

This is what my output from your program looks like; I assume it's the same for you? (In the classroom setting there may be no =>):

bill
steve
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '231321321',
  address: [ 'Legal Rd', 'Nirvania', 'AFBI', '201603' ] }
=> { firstName: 'Steve',
  lastName: 'Jobs',
  number: '231321321',
  address: [ 'Legal Rd', 'Nirvania', 'AFBI', '201603' ] }

In this case bill and steve are the result of the list function, and the first Steve Object is from your search function. If you noticed on the other console, the second Steve Object should have been a different color... I believe that is for this reason (see posts by @jibblyj, @leonhard.wettengmx.n, and @haxor789).

I believe this suggestion from Leon should make your extra output disappear if it bothers you.

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

frisby