5/6 Building Address Book


#1



Hi,
It asks me if I created a function called search, but I did create one...
Also the console.log prints out both names than jones a second time.

Can someone pls let me know what iam doing wrong?


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

search("Jones");

}
list();


#2

Ok, for starters, in the first and second "for" statement, you should declare I=0; like this:

var i =0;

not

i = 0

And also make sure the first "for" statement has an ending bracket. a see no closing bracket for it.

And, finally, for the if statement, switch it around. Instead of:

if(contacts[i].lastName === lastName)

do this:

if(lastName === contacts[i].lastName.

It should work, if not, then please contact me. Thanks!

NOTE: if you want it to print Bob Jones once, take off the "list()"


#3

@krazykodekat

This is basically the same thing.


#4

Hi and I think that you should use a for in like that

for (var key in friends)

#5

true that.:blush: Sorry, I just realize it too. :stuck_out_tongue_winking_eye:


#6

@wizmarco

The method @leftthumb used is good too. I don't see what's the problem.


#7

I changed nothing and its working...
i didnt add var or switch around the statements....
-,-


#8

I have the same code as yours it just said "There was a problem with your syntax." and couldn't console.log anything.


#9

Hi.. explain something to me!Why should he change thee..
if(contacts[i].lastName === lastName) ---> if(lastName === contacts[i].lastName. ?? I cant understand it!I do realise the missing var inside the for loop (... i=0), but i dont see any issue by using this order in the if statement!


#10

@codemaster75444

This is only the same thing.


#11

Same problem with me ..


#12

You can only determine if lastName is inside the contacts array if you loop through it contacts, but you deleted the part that loops through contacts ( i.e., printPerson(contacts[i]) ). So after you loop through contacts, then state the if statement.

code inside search function:
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {

    printPerson(contacts[i]);

    if (lastName == contacts[i]['lastName']) {
         printPerson(contacts[i]);
    }

}

//Then call search function. Bob Jones name should appear twice in the console followed by Mary Johnson. I don't know get the logic of the result. You'd think it would be as follows: Bob Jones, Mary Johnson, Bob Jones.


#13

I copied and pasted the function it turns out your problem was a } right after you called the search function


#14

I copied what you said here and don't understand the problem:

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 printPerson(contacts[i]);
}
if (lastName == contacts[i]['lastName']) {
printPerson(contacts[i]);
}
};