5. Finding That Special Someone


#1


I couldn't figure out what's wrong.

Error Message: "Oops, try again. It looks like your search function didn't log "Bob Jones" to the console. Did you remember to call search after you defined it?"


var search = function(lastName) {
    var contactsLength = contacts.Length;
    for(var i = 0; i < contactsLength; i++) {
        
        if (contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
    }
}
search("Jones")


#2

This part of code is correct, please show us all of your code


#3

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(i = 0; i < contactsLength; i++) {

    if (contacts[i].lastName === lastName) {
        printPerson(contacts[i]);
    }
}

}
search("Jones")


#4

Than you should refresh the site or clear the cache from your browser.


#5

It doesn't work with the small "l" either


#6

Upps edited the posting before yours.

Sorry look up :slight_smile:


#7

thank you

stay groovy


#8

in your 'for' loop, put 'var' before 'i'

for ( var i = 0; i < contactsLength; i++){

that is the second 'for' loop.
the rest of your code is correct.


#9

You forget put semicolons (after curly brackets)

Correct code:

var search = function(lastName){
contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (contacts[i].lastName === lastName) {
printPerson(contacts[i]);
};
};
};
search("Jones");


#10

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