1

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.

8
  • 2
    I suggest you look over the methods available on arrays. You will find indexOf which already does what you want. It will return -1 if there's no match, or an index greater than or equal to zero if there is. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Commented Jan 28, 2016 at 6:26
  • 2
    alert(["Steve", "John", "Dani", "Bob"].includes("Moe")) Commented Jan 28, 2016 at 6:27
  • stackoverflow.com/a/143863/3399676 Commented Jan 28, 2016 at 6:27
  • alert(/\bMoe\b/.test(["Steve", "John", "Dani", "Bob"])) Commented Jan 28, 2016 at 6:28
  • alert(["Steve", "John", "Dani", "Bob"].some(/./.test, /\bMoe\b/)) Commented Jan 28, 2016 at 6:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.