Push doesnt work


#1



why this doesnt work?


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;
    
    for( var i = 0; i < contactsLength.length ; i++) {
        if( contactsLength[i].lastName = lastName)
        {
            console.log(contactsLength[i].firstName +" "+ contactsLength[i].lastName);
            break;
        }
        if (i == 0)
        {
            console.log("not found");
            break;
        }
    }
}

function add( firstName , lastName, email, phoneNumber){
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = email;
    this.email= phoneNumber;
}


var kevin = add( "Jack", "levin", "test.com", "0812");

contacts.push( kevin );

list();


#2

Please tell us which lesson?
And is that your complete code?


#3

edited.....check out my code


#4

HI with that

function add( firstName , lastName, email, phoneNumber){
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = email;
    this.email= phoneNumber;
}

you create an object add instaed of a function add like the lesson aske you.


#5

this add is function right? or is it an object?
i thought is object is the variable that hold the function and the function is the part of the variable itself
i just realized i dont know whats the different hahaha


#6

it's an object then..
here that is a function

function functionName(firstName, lastName) {
    #an Example
    return firstName + " " + lastName;
}

then here an object

function Object(name, lastName) {
   this.name = name,
   this.lastName = lastName
}

#7
is this an object?i thought this is an constructor
function Object(name, lastName, Age) {
    this.name = name,
    this.lastName = lastName,         
    this.Age= SetAge(  Age ); //what do we call this one ? function or method? 
};

#8

yes, you rigth it's an object constructor which is the same did on your code..an object will be

var object = {
   name: "Halol",
   age: 15
}

#9
  function SetAge( Age ) {
  return 2014 - Age ;
  }

  function Object(name, lastName, Age) {

  this.Age= SetAge(  Age ); //so what do we call this one?method right?
  };

#10

no that will be...

#A function setAge
function SetAge( Age ) {
  return 2014 - Age ;
}

and a object

function Object(name, lastName, Age) {
    this.Age= SetAge(  Age ); //so what do we call this one?method right?
};

if you want to make a method it should be inside the object constructor like that

function Object(name, Age) {
    this.Age= Age;
    #this will the method SetAge
    this.SetAge = function(Age) {
       return 2014 - Age;
   }
}