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


#1

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);
    printPerson(contacts[i]);
search(Jones);


#2

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

#3

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]); }
search(Jones);

#4

when you have a for loop:

for (k = 0; k < 5; k++){
  console.log(k);
}

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

same for if condition:

if (true){
   console.log(true);
}

this curly brackets are missing in your case.


#5

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]); } } }
search(Jones);

#6

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


#7

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


#8

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


#9

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!


#10

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