5.) Finding That Special Someone Setting up the For and If Statements


I get the unexpected token error but I am not really sure If I have set this up right at all. The 4th part of the instruction was confusing to me so I would just like some help straightening it all out xD

var search = function(lastName);
var contactsLength = [Bob, Mary, Jones]
for( i = 0; i < (contacts.length); i++)
    if(lastName === lastName);


You didn't enclose your function in { }. You also don't need the semicolon when you name your function. Also, you should set your contactsLength to contacts.length so you would be able to feed different sized lists into it. Also, lastName will always be the same as lastName, so you want to check if contacts[i] === lastName. (The same way you're printing it.) It seems like your for and if statements are also missing the { } to enclose the block of the statement after the condition.

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


Yeah something about those { always get me xD However,
" 05. At the bottom of the file, call the search function, passing in "Jones" as the last name to search for."
New Error: 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?
New Code:

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


when you have a for loop:

for (k = 0; k < 5; k++){

what the loop should execute is between curly brackets ({})

same for if condition:

if (true){

this curly brackets are missing in your case.


Yeah I got that. I was working on that when I post the previous code. I still get the same problem of an undefined jones in my calling...

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


when calling the function, the name should be a string.


You want to search for "Jones" as a string, not as an object.


I tried that too and get the same result! I was confused and now I be more confused xD Well I haven't tried it since I fixed my { problem hang on


Ahhhh yesss It passed! Thanks! Apparently when I tried earlier it was my { issue so I just assumed that wasn't the answer but it was the answer I just need to do some background work to make it work lol Thanks both of you!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.