Building an Address Book - 6. We Made a Friend


#1


I've been stuck on this for a few days now. It says my name wasn't logged into the console. I don't understand how others have added contacts[contacts.length] = contact and the problem is solved, and why? I've tried resetting the code, refreshing the page, looking through all the posts on the forums, but I can't figure out what's wrong. I don't want the answer, just a push in the right direction, as I need to figure this out.
Thanks.



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 - 1; i++) {
		printPerson(contacts[i]);
	}
}

function search(lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength - 1; i++) {
        printPerson(contacts[i]);
        if (contacts[i].lastName === lastName) {
            console.log(printPerson(contacts[i]));
        } console.log("Nope");
    }
}

function add(firstName, lastName, phoneNumber, email) {
   contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };  
}

add("Richard", "Figueroa-Mejias", "626 862-8880", "richafig@gmail.com");

list(contacts);


#2

your last line:

list(contacts);

why does your function call have an argument? list doesn't have a parameter.

if we where to add this at end:

console.log(contacts);

we can that Richard is added, which means something is wrong with your list function, in particular with this line:

for (var i = 0; i < contactsLength - 1; i++)

what do you think it can be?


#3

You're right, if there isn't a parameter, I shouldn't add one. With list, I shouldn't have the contactsLength - 1, because it will never loop correctly, right?


#4

you shouldn't have the -1 because then the last item in the array gets skipped


#5

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