6. We Made a Friend, Invalid left-hand side in assignment


#1


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


The following error makes me get stuck and I've been here for a couple of days and ni idea on what to do, please help me!"Oops, try again. Make sure that the list() function still exists! Your code threw the following error when checking if you called list(): Error: Line 37: Invalid left-hand side in assignment"


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

function add (firstName, lastName, email, phoneNumber) { 
    contacts(contacts.length) = { // this is line 37
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber // the mistake is shown declaring the properties
    }
}
add("Rebecca", "Jones", "(555) 5555 2525", "Rebecca.Jones@Example.com");
list();


#2

You're almost there. At line 37 contacts(contacts.length) = {
you use parenthesis, but that doesn't make sense, because parenthesis are needed when you call a function, but in your case contacts variable holds an array so you should use square brackets:
contacts[contacts.length] and now you will be able to create new array item correctly.


#3

Its working now! thank you man, I cant believe I didn`t see that


#4

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