Finding that Special Someone( code goes through but it think its wrong?)


#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 < contactsLength; i++)
    {
        if(contactsLength === bob.lastName)
        {
            printPerson(contacts[0]);
        }
        else
        {
            printPerson(contacts[i]);
        }
    }
};

search("Jones");

#2

Hi @bitsurfer16879,

Let's look at this line:

if(contactsLength === bob.lastName)

Try to say it in English to see if it makes sense, like this: "If the length of my contacts array exactly matches Bob's last name then do something."

Your code is comparing a number with Bob's last name. Is that what you really meant to compare there? Try saying in English words the two things that you really want to compare.


#3

thx, now it makes sense. I'll take note about saying it in english =)


#4

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