6! we made a new friend


#1

My code does not seem to work. Can anyone help? Thanks in advance

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

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

add("Perez", "Willie-Nwobu", "ebukaperez@gmail.com", 2702934448)


#2

@perezwillz
try it with

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

#3

Hi leonhard.wettengmx.n, I just have a question, why do we bother to use contactsLength when we could use contacts.length throughout the various functions?
Or am I just not understanding the importance of it ?

Thanks in advance.


#4

@demostroyer,
Your assumption is correct......


#5

Grand , I did it that way and it worked, I just didn't see the point in using another variable when we have something that is already doing the same job.