Finding that Special Someone


#1


https://www.codecademy.com/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661#

Everything works if I remove the "else" statement that logs "Match not found" to console. I don't understand why this one line isn't working.

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"*/

function search(lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
		if (lastName === contacts[i].lastName) {
		    printPerson(contacts[i]);
		};
		else {
		    console.log("No match found.");
		};
	};
}


search("Jones")


#2

closing curly brackets of if and else shouldn't have semi-colons

I would place no match found outside the loop


#3

Thank you for your quick response!


#4

did you also place else outside the loop? You only want to log not found to the console after you had all persons in your addressbook


#5

Hi stetim94, how you mean placing the "else statement" outside the loop? Do you mean outside the "for loop"?
this is what i have in my code:

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

error message: missing before statement.. thanks/


#6

here:

printPerson "No contacts founds";

why are you using printPerson? 1. that doesn't make sense and its syntax wise not rightly used

what i mean is that if we search for Johnson, it will log no contacts founds for Jones, while it should only log this at the end (after the for loop)

it makes sense to log not found to the console after the for loop, not for every entry which is not a match (which is what you are currently doing)


#7

Could you please, show me ?
Thanks


#8

what do you want to do with this string:

"No contacts founds";

? log it to the console maybe?


#9

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