Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why is there still only two items in the contacts array? I'm guessing the problem is in the "add function," i'm trying to add a new person.

JavaScript
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"*/
function search(lastName) {
    var contactsLength = contacts.length;
    for(i = 0;i < contacts.length; i++) {
        if(contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
    }
};
search("Jones")
//------------------------------
function add(firstName, lastName, email, telephone){
    //add newPerson object
    var newPerson ={
        firstName: firstName,
        lastName: lastName,
         
        email: email,
        phoneNumber: telephone
    };
    //add object to array
    contacts[contacts.length] = newPerson;
     
}

//1.4 run
list();
//-----------------------------------------------------------
why is newPerson not adding into contacts?
Posted
Updated 27-Feb-15 12:02pm
v2

1 solution

Your approach is wrong from the very beginning. Never do such things. I'll explain why. It may not fix the problem you are reporting; I don't even want to look at it, because it would be a waste of time, first of all. You need to fix more important thing.

Look at your definition of bob. (Do you always so impolitely name people not using capitalization? It's just by the way…)
The definition is perfectly fine, syntax and everything. You could use it perfectly, if there was only one bob, or Bob. But, after that, you have mary. Poor Mary spoils everything. At this point, your code is already dead, even if it worked. Imagine what could happened if you misspelled at least one property name. It would be too hard to locate the bug. Here is what you want to do instead:
JavaScript
var Person = function(firstName, lastName, phoneNumber, email) {
   this.firstName = firstName;
   this.lastName = lastName;
   this.phoneNumber = phoneNumber;
   this.email = email;
}

// ...

var contacts = [
   new Person("Mary", "Johnson", "(650) 888-8888", "mary.johnson@example.com"),
   new Person("Bob", "Jones", "(650) 777-7777", "bob.jones@example.com")
];

Can you see the idea? You re-use creation of person and avoid the problem I mentioned. There is no need in the pointless function add.

Moreover, you can check up if two objects came from the same constructor:
JavaScript
var sameTypes = contacts[0].constructor === contacts[1].constructor; // will return true
// assuming your old bob definition was there:
var differentTypes = contacts[0].constructor === bob.constructor; // will return false
// even though the objects are logically equivalent;
// also, it won't throw an exception: bob object does have a constructor

Don't understand my use of the word "types" literary: they are nor the types in the sense of JavaScript nor in the sense of OOP (nothing in JavaScript is really OOP; the concepts are fundamentally different).

Please understand that my suggestion is not forcing you to use different style; it's much more fundamental: I suggest you to avoid so brutal violation of more fundamental principle: Don't Repeat Yourself: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

Please start from this point and see if you can cope with that simple problem and everything else.

—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 28-Feb-15 7:48am    
I though I explain you the danger and illiteracy of the beginning of the code. It's possible that they explained you some syntax/usage but not expected you to repeat the same on a different object. You can always read documentation yourself and see what happens. In other words, you need to act rationally, not just do what people told you. (And, but the way, fake education or just bad teaching is also pretty usual, I face it from time to time.)

And thank you for understanding.

—SA
UWStudents 28-Feb-15 7:55am    
Do you still recommend me using codeacademy?
Sergey Alexandrovich Kryukov 28-Feb-15 7:56am    
I don't have enough information to recommend it or not.
—SA
UWStudents 28-Feb-15 7:57am    
Ok, I will just go ahead an finish that JS course and probably move on to some other site.
thanks alot.
Sergey Alexandrovich Kryukov 28-Feb-15 7:55am    
No problem. I advice you re-write code the way I suggest and move further.
Your further questions will be welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900