Why is this code wrong?6/6


#1

My code is..

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(i=0;i<contacts.length;i++)
{
    if(contacts[i].lastName===lastName)
    {
        printPerson(contacts[i]);
    }
}
};
search("Jones");
var add=function(firstName, lastName, email, phoneNumber)
{
    this.firstName=firstName;
    this.lastName=lastName;
    this.email=email;
    this.phoneNumber=phoneNumber;
};
contacts.push(add);
add("tim","cails","00ga@gmail.com",901902);
list();

by the way, i know that the solution is..

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(i=0;i<contacts.length;i++)
{
    if(contacts[i].lastName===lastName)
    {
        printPerson(contacts[i]);
    }
}
};
search("Jones");
var add=function(firstName, lastName, email, phoneNumber)
{
    this.firstName=firstName;
    this.lastName=lastName;
    this.email=email;
    this.phoneNumber=phoneNumber;
};
contacts.add=add;
contacts.add("tim","cails","00ga@gmail.com",901902);
contacts[contacts.length]=game;

list();

or, this works too..

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(i=0;i<contacts.length;i++)
{
    if(contacts[i].lastName===lastName)
    {
        printPerson(contacts[i]);
    }
}
};
search("Jones");
var add=function(firstName, lastName, email, phoneNumber)
{
    this.firstName=firstName;
    this.lastName=lastName;
    this.email=email;
    this.phoneNumber=phoneNumber;
};
contacts.add=add;
var game=new add("tim","cails","00ga@gmail.com",901902);
contacts[contacts.length]=game;

list();

but how do i utilize array.push(); here?


#2

Remember that when we call the add function it adds a Person object to our list of contacts. SO the hint is to create an object in the add method and give it all those attributes you have assigned and then now all you have to do is add that object to the list


#3

i know i should get a syntax error for that. but at least, i should have got a proper output.

because we are adding an object to an array. using the push method(?)


#4

Instead of, contacts[contacts.length] = {};

we would write,

contacts.push({});

Of course, the object would be filled in.

If we are to go one step further and create a Person constructor, then,

function Person(first, last, email, phone) {
    this.firstName = first;
    this.lastName = last;
    this.email = email;
    this.phoneNumber = phone;
}

Then our add() function would look something like this:

var add = function(firstName, lastName, email, phoneNumber) {
    contacts.push(new Person(firstName, lastName, email, phoneNumber));
};

#5

Why is everyone talking about .push if we haven't covered that yet in the lessons? :neutral_face:


#6

Array.prototype.push() was introduced in the Search Text for Your Name challenge that was paired up with the 'FOR' LOOPS IN JAVASCRIPT module. Admittedly, it hasn't come up in any lessons, but once we cover it in the challenge, it's pretty much moot after that.