mercredi 27 janvier 2016

How do I use an if statement to search an array [duplicate]

This question already has an answer here:

I have this challenge which in my head seems easy enough, but I can't seem to get my function right.

The challenge is as follows:

Create a function that determines if a given name is amongst the names.

So here is my array and function -

 var family = ["Steve", "John", "Dani", "Bob"];

 function searchName(f){
   for (i=0; i<family.length; i++){
     if ((family.lastIndexOf(f) != family[i]))
       {family[i]}
   }
 }

 searchName("Moe");

Now all this returns is undefined. Which is better than errors I suppose. What I want it to do is when I pass through a name that is not in the array to have it return a false statement, and if a name that is in the array, true. I also know that I need to throw in .toUpperCase and .toLowerCase in there as well to account for the case sensitivity. My assumption is that rather than just an if statement, I need an if/else if/ else.

Aucun commentaire:

Enregistrer un commentaire