Right This Time - But Can't Tell WHY?


#1

Continuing the discussion from Printed what was asked, but marked wrong:

Please forgive the length here, but this section has been confusing.

(I realize the problem may be as some have suggested - 1 or more bugs somewhere here).

But if that’s not the case…

I can’t figure out any reason, and would appreciate if anyone can explain - why only one of the Object Coding structures we’ve been taught ends up being marked correct in this exercise?

I modified the Object Code according to Leon’s suggestion (thank you Leon) and needed to fix the search function before it would work - but (unless I’m missing something obvious & would appreciate it being pointed out if I am) – with the search function corrected – it should have worked with my original Object Code formatting - which worked and was marked correct in multiple exercises right before this one.

--- RESULTS ARE THE SAME, IN BOTH CASES, WHETHER LIST FUNCTION IS LEFT IN, OR REMOVED. ---

CODE: (Partially Correct, Output below):

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "bill",
friends.bill.lastName = "Gates",
friends.bill.number = "(206) 555-5555",
friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];

friends.steve = new Object();
friends.steve.firstName = "steve",
friends.steve.lastName = "jobs",
friends.steve.number = "(408) 555-5555",
friends.steve.address = ['1 Infinite Loop','Cupertino','CA','99999'];

var list = function(friends) {
   for (var firstName in friends) {
      console.log(firstName);

}
};

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

list(friends);
search("Steve");

OUTPUT: (Object Code recognized, and used, by the list function - but not by the search function):

bill
steve

CODE: (Completely Correct, Output below):

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']
},
};

var list = function(friends) {
   for (var firstName in friends) {
      console.log(firstName);

}
};

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

list(friends);
search("Steve");

OUTPUT: (Object Code recognized, and used - by both functions):

bill
steve
{ 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"]}


#2

Steve
steve

different names.


#3

No, sorry, meant to mention that too (been through this for about 5 days, more combinations than I can recall) - but while changing the 1st letter's case, does give this:

Output:

bill
steve
{ firstName: 'steve',
lastName: 'jobs',
number: '(408) 555-5555',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '99999' ] }
{"firstName":"steve","lastName":"jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","99999"]}

It also gives this Error Message:

Oops, try again. It looks like your search function doesn't return contact information for Steve.


#4

And there's no Steve there.
You have a steve instead.


#5

An aside with regard to variable naming semantics:

The above code uses a variable name, firstName which is kind of misleading. It doesn't represent the first name, but a primary key of the friends object. The real first name is a property of the object associated with each key.

    friends.steve            // primary key in friends
    friends.steve.firstName  // primary key in friends.steve

So a more semantic variable name in this instance would be key. Furthermore, when the parameter of a function is intended to be a reference object (array, object or function) then we should not use the exact name, but a generic local variable to act as a reference to the object. Following this reasoning, we get,

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

This now becomes a fully reusable function that can fit nicely in the utility drawer. It is completely generic (as long as there is no global object with the name, obj). When we use the name friends, that blocks any other object from being able to use this function.


#6

I saw (obj) in a reply you gave to someone else & tried it, but must have had something else wrong, it didn’t work then, but it worked now, thanks for the suggestion.


#7

There's no Steve there, but “Steve” (with Capital) only appears in the Error Message – there are no names mentioned to be specifically searched for in the instructions, it just says (paraphrasing) – “we'll add a search function, which will take a first name as an argument. If it finds a match (to whatever name we use) it will log our friend's contact information (firstName, lastName, number, address) to the console.”

So unless it’s something we should just understand would be a given, I wasn’t expecting it to be expected that a 1st letter Cap was required, so I was just re-using my code that carried over from the previous exercise, where it was marked correct.

But that seems to be the case here – I just retried my original code, but Capitalized the 1st letter, and now it also works.