#5 finding that special someone - prints out too many things


#1

Hi,
My code prints out both people and I can't figure out why.
Bob Jones
Mary Johnson

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]);
    }
}

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

search("Jones");

#2

@noisynora i will find your problem soon but if you could format your code using http://dirtymarkup.com Would be nice


#3

Ok your problem is that if statement, you didnt understand what the condiotion is suppose to be. It specifically said in the instructions:

add an if statement that checks to see if the lastName property of the object is equal to the lastName argument.

So if you implement that to code:

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

And your not suppose to have an else statement for this task #5. Hope this helepd! And Please Checkout http://dirtymarkup.com


#4

thank you.
I cannot get the formatting to work no matter what. It works on the Dirty Markup site but copy pasting to this site brings it back to how it shows.


#5

Ok can you try http://jsbeautifier.org


#6

The way to make the formatting show up is to use the </>
Thank you!


#7

Its your if statement

if( (contacts[i].lastName) === lastName) {
etc..


#8

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 {
if((contacts[i].lastName)===lastName)
{
printPerson(contacts[i]);
}
}
};
search("Jones");