5/6 Stuck


#1

Not sure how to make it right...
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(person.lastName === search(lastName)){
printPerson(contacts[i].lastName);
}
}
};
search("Jones");


#2

I would say the problem is with your "if" condition.

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

In the 'if' condition you want the value you'll be entering for "lastName" (when you call it) to equate to === any matching item found in the lastName values ".lastName" of the contact array "contacts" which will be gone through using the for loop defined by variable i.
Hence: if(last Name === contacts[i].lastName) and the conditions are met, we want to print matching contacts using the printPerson function previously defined.

Note the instructions ask you to print the first and last names so no need to use .lastName.

Hope this helps. It made sense as I was typing it!


#4

There's a fault in your code,

var search = function(lastName) {
for (var i = 0; i < contactsLength; i++) {
if(lastName === contacts[i].lastName) {
printPerson(contacts[i]);
};
};
};

this one works the best ^


#5

Thanks for pointing it out! I clearly do not work the shift key very well!
Obviously, the for loop should read

for (var i = 0; i

NOT as I originally put
for var i = 0; i,contactsLength; i++) {

Should have checked my typing before I posted!
Thank you!


#6

I am not sure all the above is enough for a working solution. My code produces no syntax errors, however it doesn't log "Bob Jones" to the console. Here is my 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);
};
/*Create a search function
then call it passing "Jones"*/
function search (lastName){
var contactsLength = contacts.length;
for (var i = 0; i if(contacts[i].lastname === lastName)
{printPerson(contacts[i]);
};//end print
};//end for loop
}; //end search function
search("Jones");


#7

The for loop is supposed to be like this:

for (var i = 0; i < contactsLength; i++) {

I just finished the level myself. :smiley:


#9

Thanks, actually that's what I have, but when I pasted my code into the discussion forum I notice that the preview pane on the right shows different code to the code I just pasted on the left,(it was missing the code that you pointed out)! After some messing around I discovered that it there needed to be a space after the > symbol in order for the code in the right preview pane to reflect that code I had pasted in the left pane. Here is my code again. It still has the same error when I run it.
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);
};
/*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]);
};//end print
};//end for loop
}; //end search function
search("Jones");


#10

Just showing you what I did, you can compare:

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

search("Jones");


#11

Thanks that fixed it - the problem with my code seemed to be in the line
{if(contacts[i].lastname === lastName)
whereas you had
{if(lastName === contacts[i].lastName)
so it would seem that the order of these terms is important.


#12

Most likely. :smiley:


#13

Just a quick question what does passing [i] to contacts do exactly... like what is it expressing? Essentially the for loop is saying ok... i currently is equal to zero... while i is less than the total length of the contacts array (in this case... its 2) iterate (loop through) the list and search for "Jones".

But where i get lost is here...

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

what exactly is contacts[i].lastName saying?
and contacts[i]; ?

is it saying ok... when the variable i finishes looping... and if it meets the conditions ... lastName strongly equaling the results of a person within contacts... print that person within contacts after the loop finished?

Thanks


#14

As you can see, it's contacts[i].lastName. Let's think about it. There are 2 people in our contacts array. Mary and Bob. So what i is doing is that you first search a last name, like "Jones". Then, what i does is that it searches your contacts and looks if there is someone with the last name with Jones. In this case, it is Bob, so it will print out the full name of Bob Jones.

Hope it helped! :smiley:


#15

You can try this :grinning:

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

search("Jones");

#16

jebalert -
If I am correct, contacts[i].lastName is saying: look at the last name of each of the iterations of the contacts.

printPerson(contacts[i]); is saying: do the "printPerson" function (which is to log the first and last name of the contact) for all of the iterations of contacts. Since it is within the condition that the last name is being limited to whatever is entered for Search (in this case "Jones"), it will only do so for that contact.
Hope that helps! :smile:


#17

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

search("Jones");