Finding that special someone >>> HELP ME


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
};

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
};
};

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName){
var contactsLength = contacts.length;
for(var i = 0; i < contacts.Length; i++){
if(lastName === contacts[i].lastName){
printPerson(contacts[i]);
};
};
};
search("Jones");

WHY IS MY CODE NOT WORKING?


#2

Because you used for loop but you need for in:

for(var prop in contacts){
  if(contacts[prop].lastName === lastName){
    printPerson(contacts[prop]);
  }
}

From MDN:

The for...in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.


#3

Can i give a dislike?

From the instructions

3. Create a for loop that runs through all of the items in the array. For this step, the code for search is identical to that of list

And i think a for .. in and the for statements are both loops

@syntaxity
Look at this two lines

var contactsLength = contacts.length;
for(var i = 0; i < contacts.Length; i++) // contactsLength or contacts.length with a small l  :)

#4

Of course, you can. This is my mistake, because I thought it's:

Of course, for...in, for are both loops. I didn't check what instruction says :(((


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.