Building an Address Book. 6. We made a friend


#1

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

<

Seems i'm getting blind were does my code go wrong?
Error message: Oops, try again. There was a problem with your syntax. , SyntaxError: Expected ';'

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

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

    
    
    add("Jean", "Meijers", "(020) 888-89023", "jean@example.com");
    list();
    };

#2

Got it, i've shouldn't have declared var contact[contact.length].
removed some brackets and it worked.

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

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

    
    
    add("Jean", "Meijers", "(020) 888-89023", "jean@example.com");
    list();

#3

I'd have run it in another interpreter to get a line number:

$ xsel -b | node    # feed clipboard content to nodejs
[stdin]:41
    var contacts[contacts.length] = {
                ^
SyntaxError: Unexpected token [

Use whatever interpreter is handy.. Your browser, something online, nodejs


#4

By stating "var contacts, your are your creating a new variable other than the existing contacts that
you are expected to access from the global scope.

your code should be
contacts[contacts.length]


#5

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