We Made A Friend! (6/6) Not printing - please halp!


#1

It's been a struggle and even though I'm getting through the mission with this code, it's not printing anything except for an error "SyntaxError: Unexpected token .".

Can anyone please help me understand why this is not printing? (my code below)

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 alex = {
    firstName: "Alex",
    lastName: "Jones",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary, alex, add];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

//Create an add function adds record to contacts
function add (firstName, lastName, phoneNumber, email){
    contacts[contacts.length] = {
        this.firstName: firstName;
        this.lastName: lastName;
        this.email: email;
        this.phoneNumber: phoneNumber;
    }
};

//Create a list function
function list() {
var contactsLength = contacts.length;
for (var i = 0; i = contactsLength; i++) {
printPerson(contacts[i]);
}};


add("This", "cant", "work", "can it?");
list(contacts[i]);

Thanks for anyone who can spot my mishap(s)!
Alex


#2

There should only be two named object references in your array, bob and mary. The rest are added to the array anonymously.

The above is an assignment of a plain object literal, and is not a constructor so has no this object. Remove this. from all the above lines.


#3

Thanks @mtf, I've tried those changes but am getting the same error and still no print out. I imagine the number of contacts is not the issue, but I've tried it anyhow. No luck.

Do you have a working code for this exercise which I can try? I'm not concerned with 'passing' I just want to see/understand a working solution before I move on.

I ended up removing the "this." as you suggested but still no resolve. I also see an example where another person included ".this" with 'more' success than I had. Here is their code which at least prints out the names as it is supposed to. It works 'better' but it still returns an error unfortunately.

'''
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, add];

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 add(firstName, lastName, email, phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
contacts[contacts.length] = this;
}

add("Biff", "JoeBob", "breal@glah.net", "(555)555-5555");
list();

'''

Thanks again for taking the time to look at my code and let me know what you think.
Alex


#4

What you are actually adding to the array is a whole lot more than what you might think. There is a tonne of stuff in the this object. If you wish to use a constructor, then use new to strip everything away and leave only the direct properties you assign.

contacts[contacts.length] = new add( ... );

Or

contacts.push(new add( ... ));

Only thing now, add doesn't make for a very good Class, not like Contact would.

function Contact( ...) {
    // only direct properties
}

And your add function could be,

function add( ... ) {
    contacts.push(new Contact( ... ))
}

Remember to remove that array assignment in your constructor.

What's neat about this is now all your array elements inherit the constructor.prototype object. I would be sure to add bob and mary this way, as well, and not as object references. They won't inherit from Person.prototype otherwise.

We can now write methods on the prototype that each instance can run on themselves. Our search function could be a Class method, but printing individual objects could be an instance method. Something to explore when you get to the end of this track.


#5

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