#6. We made a Friend


#1


Please help with the code. I keep getting a reference error 'sam is not defined'

6 we made a friend


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]);
        };
        
    
    };
};

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


add(Sam, Smith, "(650) 222-2222", "sam.smith@example.com");
list();


Replace this line with your code.


#2

here:

add(Sam, Smith, "(650) 222-2222", "sam.smith@example.com");

why are Sam and Smith variables? All the other arguments you pass into this function calls are string, why aren't sam and smith?


#3

I did not make those 2 items a string because the instuctions asked us not to do so.


#4

If you feel it would eliminate the error code I will be happy to do so.


#5

that is for the add function itself, not the function call. Speaking of the function:

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

phoneNumber and email shouldn't be strings here, so that the phoneNumber and email properties get the value from the function parameter and not a fixed string value


#6

I kept getting syntax error codes until I made those2 items strings.


#7

can i see an updated version of your full code?


#8

I made the change you questioned and seemed to suggest. the code ran without difficulty.


#9

do you understand it?


#10

    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);
}
var bob = {

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]);
        };
        
    
    };
};

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


add("Sam", "Smith", "(650) 222-2222", "sam.smith@example.com");
list();

I do not understand how the editor would allow the differences in the array and what I put in the add function. Also, var bob = { is at the top.


#11

I would also add that it is not totally clear to me how making those two items a string cleared the error code, unless it is because it now fits the array 'model'.


#12

using the add function we simplify the process of adding more people to our address book. If you now want to add a 4th person, a simple function call is all you need

each contact is an object containing the following information:

first name
last name
phone number
email

then we store the objects in a list so we have a "book" with our contacts


#13

I understand that part. I am not clear why making all the items in the add function a string is necessary for 'sam' to be 'defined'!


#14

well, if you don't make it a string, it would be a variable. But variable need to be defined before you can use them, and you didn't define a variable named sam anywhere


#15

Thanks. I get it. Thanks for all your help.


#16

here:

        phoneNumber: "phoneNumber",
        email:"email"

are you sure you want to use strings here? By using strings, Sam Smith his phoneNumber will be phoneNumber and his email will be email

here you should use variable (the function parameters) to get the values you supply at function call


#17

As I wrote above, if I removed the quote I had syntax errors at every level of the array saying 'you need a ; before this statement'. Adding the quotes and thus making those two items was the only way the syntax errors would go away. I did not understand but did it to avoid the syntax errors.


#18

I understand your point. Forget my earlier comment. Thanks again.


#19

here:

var bob = {

function list() {
	var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

what is var bob = { doing there? that is where i get syntax errors from. Once i place var bob = { correctly, its all good

Post an updated version of your code if problems persist


#20

No, it ran fine, thanks.