Printed what was asked, but marked wrong


#1
  • (and even more bizarre is that it's marked wrong and then prints nothing (same in all 3 browsers) when I capitalized the name's 1st letters).

Code:

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 search = function(name) {
for (var firstName in friends) {
if (firstName === name) {
console.log("steve " + "jobs" + " (408)555-5555"
+ " 1 Infinite Loop, Cupertino, CA, 99999");
return "steve " + "jobs" + " (408)555-5555"
+ " 1 Infinite Loop, Cupertino, CA, 99999";
}
}
}
search("steve");

Output: steve jobs (408)555-5555 1 Infinite Loop, Cupertino, CA, 99999
"steve jobs (408)555-5555 1 Infinite Loop, Cupertino, CA, 99999"

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

I understand now the duplication of printing, but why marked wrong if it generated what exactly was instructed?

Thanks,

Robert.


#2

@manhattanalchemist,

var friends= {
              bill: { firstName: 'Bill',
                      lastName: 'Gates',
                      number: '444-444-4444',
                      address: [ 'One Microsoft way', 'Redwood', 'WA' ]
              },
             steve: {
                      firstName: 'Steve',
                      lastName: 'Jobs',
                      number: '333-333-3333',
                      address: [ 'Two infinite loop', 'Bentown', 'DV' ] 
              }
   };

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation == only literal property-key allowed

friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation == literal & VARIABLE

1 using the literal property-key

friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]
in this case you will get the string VALUE of the lastName property
of the associated object VALUE of bill


Right This Time - But Can't Tell WHY?