5. Finding that Special Someone


#1

Hi all, when I enter this code I get the error: > 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 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(lastName === contacts[i].lastName)
        {
            printperson(i);
        }
        else
        {
            "something went wrong"
        };
    };
}

search("Jones");

Cheers


#2

Under if condition, try putting ---- printPerson(contacts[i]);


#3

Thanks for your suggestion @yuvrajcode

You are right. I also had a mistake on my code in the line about my for loop

var contactsLength = contacts.Length;

It should be

var contactsLength = contacts.length;

silly mistake to make :stuck_out_tongue:

Here's my final solution for this problem, working as required

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< contactsLength ; i++)
    {
        if(contacts[i].lastName === lastName)
        {
            printPerson(contacts[i]);
        }
        else
        {
            "something went wrong";
        };
    };
}

search("Jones");


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