# 5 Finding That Special Someone - can't figure out what I'm doing wrong


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

Keep getting: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?
And: ReferenceError: Jones is not defined. Isn't Jones defined as a last name?


#2

for (var i = 0; i if (contacts.lastName = lastName) {
 printPerson(contacts[i]);
 }

Remember, for loops are as follows:

for ( initialization ; condition ; increment / decrement "Postfix") { ... }

You had it right earlier on in your code, so just fix that and we'll see what's going on from there.


#3

Apologies. I didn't copy it right. The correct sample is:

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

#4

Hey @alexeld! Your problem is in your if statement, you are suppose to have 3 or 2 equal signs to check for comparison. 1 equal sign is an assignment Here it is:

Just put that as two equal sign and you should be fine! Hope this helped!


#5

if (lastName =   contacts.lastName) {
              ^^

Right now you have an initialization operator " = " instead of a comparative operator " == ".


#6

i still get the same errors :confused:


#7

Could you post your updated code? This will help us locate any other issues.


#8

Sorry if this is getting repetitive, I really appreciate the help a lot

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


#9

Hey you forgot to add you array index here:

It is suppose to be:

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


#10

Well I want to help, but I'm having a little bit of difficulty concentrating at the moment,

I can suggest however that you use a "for-in" loop, instead of what you're using at the moment.

for ( var variable in object ) {
   //Code here
}

More specifically

for ( var variable in contacts){
    if (contacts[variable].lastName == lastName) {
        printPerson(contacts[variable]);
    }
}

If that doesn't work try using "firstName" instead as that's what you're putting in the contacts array.

Or follow @amanuel2's advice. His is more direct.


#11

And i forgot to mention, when you called your function your suppose to put it in quotes.
So instead of:

Do this:
search("Jones");


#12

Thank you. I did forget to define Jones as a string in the search("Jones") function. And once I followed the first line of advice, it went through perfectly.
Again, thank you. This was really frustrating me. Do you know of any resources where I could better learn the nuances?


#13

I had the same problems, But really you can go back and try the earlier lessons again. Just doing it as a refresher is time consuming, but super worth it


#14

I mean there are many resouses! Its the Internet :smile:!! Like this is a good place to learn Javascript.

It has a ton of javascript frame works + Original!


#15

Yeah... I shouldn't ignore the importance of review. You've just convinced me. Thank you for the help again.


#16

Oh wow...That's a lot of videos. Thank you for helping me solve this. I'll take a look at all of them :smile:


#17

Why do we have to type contacts[variable].lastName instead of just contacts.lastName? I mean variable [i] just stands for i = 0 and it's made for loop to work properly. Why can't array find lastName without [i] variable?


#18

Did you ever find out? If not, I'll help since I'm not very sure either.


#19

Was just going to say that! Jones is a string and thus needs "" marks.


#20

My guess is that the "i" is needed for the loop to function in the first place so the program can identify whether the word we type when we call the search function conforms with one of the values in the array. Without running the loop, there is nothing to tell the program to look through everything in the contacts array. Someone feel free to correct me if I am wrong.