Help 7/8, can someone explain what I'm doing wrong


#1

I have followed the instruction in the hint section and looked back on the first exercise as an example. But i keep getting an error or my search function doesn't work.
Here is my code.

var friends = new Object();

// friends.bill = {};
// friends.steve = {};
// friends.dave = {};

var friends = {
    bill: {
        firstName:"Bill",
        lastName: "Gates",
        number: "555 555 1234",
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "555 555 1235",
        address: ['1 Infinite Loop','Cupertino','CA','95014']
    },
    dave:{
        firstName: "Dave",
        lastname: "Watts",
        number: "0208 555 1234",
        address: ["Number 1 Dave's Street", "London","UK","NW1 0NA"]
    },
};

var list = function (obj) {
    for (var entries in obj) {
        console.log(entries);
    }
};

//list(friends);

var search = function (name) {
    for (entries in friends) {
        if(friends.firstName === name) {
            console.log(friends[entries]);
            return friends[entries];
        } else {
            return "no match found";
        }
    }
};

search("Steve");

The console returns
"no match found"

But I get this error when i submit it.
"Oops, try again.
It looks like your search function doesn't return contact information for Steve."

Could someone please explain what I've done wrong?


#2

Not needed for starters. Still reading...


#3

In dynamically generated (template) code, we would leave that comma there, but in literals we don't. It's not doing any harm but doesn't belong, all the same.


#4

Creative, yes, but is it meaningful? Objects have property names, which in associative array terms are keys. Would it not be easier to read with just a generic term like, key? Not to be critical; only to shed light on semantics. Objects don't have entries, per se. Tables do. It's a good term, though, all the same, just a little skewed from some readers' perspective.


#5

This won't find any matches. friends does not have a fistName property.

        if (friends[key].firstName === name) {

There are two layers we need to consider. The primary layer, those that are direct keys of friends, and the secondary layer which make up the properties of the object associated with each primary key.


#6

Thanks, I've rewritten my code.

var list = function (obj) {
    for (var prop in obj) {
        console.log(prop);
    }
};

list(friends);

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

search("Steve");

this seems to work when i submit, it is accepted but i get a weird console print out.

bill
steve
dave
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555 555 1235',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555 555 1235',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555 555 1235',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '555 555 1234',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Steve","lastName":"Jobs","number":"555 555 1235","address":["1 Infinite Loop","Cupertino","CA","95014"]}

It seems to print the search for "Steve" three times.

If i search for "Adam" the code is accepted and i get a console print out of.

bill
steve
dave
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555 555 1235',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555 555 1235',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '555 555 1234',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }

I would like to understand what happening here, if anyone could help that would be great.


#7

Your code is well done. The extraneous output is due to the number of tests that are run by the SCT. Only the first two are yours. The one that logged in the function, and the one that was returned (echoed by the console as a response).


#8

Also, for this lesson, disable the call to list(). (// comment it out)


#9

I can't be certain, but I believe the last lesson (or the first) does not have an SCT so the output is not tampered with or tested. Copy this code to one of those lessons (save them to your machine, first) and try it there.

You have a couple of final options as to when to print the return value, before or after the return. Since this is a search function, I would suggest we return the result and print it afterward. That means commenting out the console.log() statement in the function.

Now we log only the return value.

console.log(search("Steve"));

Without the SCT getting in the way we prevent any final echo since our last command is to log something.