5. Finding that special someone


#1


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


My console shows the correct record: { firstName: 'Bob',
lastName: 'Jones',
phoneNumber: '(650) 777-7777',
email: 'bob.jones@example.com' }
but includes the curly brackets (not sure if they are supposed to show in console).

I also get the following 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(var i = 0; i < contactsLength; i++) {
        if  (lastName === contacts[i].lastName){
            console.log(contacts[i]);
        }
    }
};
search("Jones");


Thanks for your insight in advance!!!


#2

You need to use your printPerson function here. Making it printPerson(contacts[i]);.


#3

Thank you so very much, for your rapid solution to my error. Cheers!


#4

Why does it return "Bob Jones" AND "Name not found" in the console ?

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) {
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
        else {
            console.log("Name not found");
            }
    }
};
search("Jones");

#5

The "for" loop is not going to stop once it finds a match. In this instance, after the loop finds a match and displays "Bob Jones", it then loops back and compares "Jones" to the "Mary Johnson" record, which of course, doesn't match. Therefore, the "else" part of your if/else function is displayed.

To solve your issue, simply remove the "else" portion of your code.


#6

Aaaaah Okay ! Thank you very much !


#7

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