Having trouble with list function in lesson 6


#1

I have written:

var friends = new Object()
friends.bill = new Object()
friends.steve = new Object()
friends.bill.firstName = "Bill"
friends.bill.lastName = "Gates"
friends.bill.number = "(012)-345-6789"
friends.bill.address = ['One Microsoft Way','Redmond','WA','98052']
friends.steve.firstName = "Steve"
friends.steve.lastName = "Jobs"
friends.steve.number = "(987)-654-3210"
friends.steve.address = ['One Infinite Loop', 'Cupertino', 'CA', '94302']

var list = function (friends)
{
for(var key in friends)
{
console.log(friends[key]);
}
}

This doesn't pass, and the console log shows:
{ firstName: 'Bill',
lastName: 'Gates',
number: '(012)-345-6789',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(987)-654-3210',
address: [ 'One Infinite Loop', 'Cupertino', 'CA', '94302' ] }
["One Infinite Loop","Cupertino","CA","94302"]

I'm surprised that the console shows anything, since the code doesn't actually execute the function (ie we don't have a separate list(friends); line - if we do, it prints all that stuff twice). I also don't see why brackets appear in the output. And in general, it seems awkward to define firstName, lastName, etc separately for each friend object. I would rather have a single kind of object, bookEntry, with a number of different fields:
id (steve, bill)
firstName (Steve and Bill)
lastName,
etc.
As we're shown to do it, we have numerous different objects that don't have to have the same structure. It feels very awkward.

Thanks for the help.


#2

Not in every exercise, but often the Submission Correctness Test (SCT) will make a call to our function (the SCT can run our our script) and scan the screen data to check for required output elements. This would appear to be one of those sort. Consequently there is a lot of screen verbosity.

It can be ignored, but error messages cannot. Please post the error message you are seeing from the SCT. I have not run your code but on quick scan it doesn't look to contain any syntax errors, apart from missing semi-colons at ends of statements.

To get around the SCT output, consider changing the name of your function for just long enough to test on your own (ignoring error messages from the lesson checker). We cannot ignore syntax errors thrown by JavaScript. Those that appear in the console in red text are all fatal errors.

Eg.

var lst = function (obj) {
    for (var key in obj) {
        console.log(obj[key]);
    }
};
lst(friends);                 // call expression

The SCT errors can be ignored. The output is the value of each property in friends. Those keys are bill and steve. Their values are objects, hence the verbose output (not confused with earlier mentioned lesson check).

The lesson instructions actually only ask for the keys.

    console.log(key);

Now the output is,

bill
steve

(in any order). Once you're sure the code runs as expected, change the function name to list and comment your call expression. Then submit.


#3

Thanks for the prompt and detailed reply. It helps to know that the test sometime runs the code, too.
By just printing out the key, and not friends[key], the code now passes. The only problem I had earlier was not printing out the right results.