Questions about javascript-"we made a friend"


#1


Hi all, 
I've been in trouble with my code for a few hours. Can someone help me with that?

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 (var i = 0; i < contactsLength; 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[contacts.length] = {firstName, lastName, email, phoneNumber};
};
add("Jack", "Johns", "jack.johns@example.com", "(241) 784-2345)");
list();

Is there something wrong with the add function? I get errors telling that "Oops, try again. Make sure that the list() function still exists! Your code threw the following error when checking if you called list(): Error: Line 45: Unexpected token ," I know it would be fine if I change the add function like this: 
  contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber
};

But, what's the difference?


#2

Your code looks fine but to be able to request users input, I have written a set of code using the prompt method to add more contact details.

var add = function(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: prompt("what is the firstName:", firstName),
        lastName: prompt("what is the lastName:", lastName),
        email: prompt("what is the email add:", email),
        phoneNumber: prompt("what is the phonenumber:", phoneNumber)
        };
    }

add(); //calls the add function.

#3

Thanks. But I think I still get that error. I don't know why. Should I just ignore that?


#4

Please post full new code. Maybe I or you missed something.

Thank you! :slight_smile:


#5

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

var add = function(firstName, lastName, email, phoneNumber) {
// contacts[contacts.length] = { //
// firstName: firstName, //
// lastName: lastName, //
// email: email, //
// phoneNumber: phoneNumber //
// }; // this block of code works fine, but I used the ones below which generates error.

this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
contacts[contacts.length] = {firstName, lastName, email, phoneNumber};
};
add("Jack", "Johns", "jack.johns@example.com", "(241) 784-2345)");
list();


#6

Okay! Thank you.

Sorry I'm replying this late. Still based on my initial suggested code so that user can make input and/or add, here is the full code now:

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) {
    for(i = 0; i < contacts.length; i++){
        printPerson(contacts[i]); // remove this line
        if(contacts[i].lastName === lastName){                
            printPerson(contacts[i]);
        }            
    }      
};
search("Jones");

var add = function(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: prompt("what is the firstName:", firstName),
        lastName: prompt("what is the lastName:", lastName),
        email: prompt("what is the email add:", email),
        phoneNumber: prompt("what is the phonenumber:", phoneNumber)
        };
    }

add();
list();

Once again, sorry for replying this late.

Hope this resolves. :slight_smile:


#7

Ok. Got it!
Thanks a lot!


#8

You're welcome.

Have fun coding! :slight_smile:


#9

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