Address book search (Debugg Needed)


#1


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


#2

You have to compare here? not assign?


#3

How would that be done then?


#4

Back in previous chapters we've got introduced to comparison operators.
There was one called Equality operator === ?


#5

Oh yeah! That's right!
Here's my updated code:
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 (var i = 0; i< contacts.length; i++) {
if (contacts.lastName === lastName){
contacts[i].lastName;
}
}
}
search("Jones");


#6

Is it still having issue?? what error youre getting?


#7

Oops, try again. It looks like your search function didn't log "Bob Jones" to the console. Did you remember to call search after you defined it?


#8

This line need to be fixed.

as you looping over contacts ,then It will be like contacts[0] , contacts[1]
so just writingcontacts.lastName ( contacts is an array) won't give you last name right ?

we also need to fix this line..

contacts[i].lastName;
we have to print name?
so we can do same thing that we did while creating list function ?


#9

How would this be done?


#10

Look contacts[index].lastName will give you last name?


#11

I'm still not sure how this would be fixed.


#12

Its okay we need to revise things again.

contacts is an array so it does not support dot notation to access its items.
we have to throw index so we get the items ,an we can see that bob and mary are objects so we can use dot notation on it and get the lastname.

contacts[0].lastName is "Jones"
contacts[1].lastName is "Johnson"

ring a bell ?
here things are easy as we're looping over contacts so we can use contacts[i] ?


#13

Could you give me an example of this?


#14

Can you explain what this line does?


#15

I think it's asking if the last name in contacts is the same as the last name being searched.


#16

But contacts is an array right?


#17

Yeah. But doesn't it contain the variables "Bob" and "Mary"?


#18

Can you tell me what you do to access the items of an array ?
do you pass numbers (index) or do you use dot notation ?


#19

Use dot notation...?


#20

Well no! so we use index to access elements of an Array.
dot notation or bracket notation is used on Objects .