Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Simon Lovelock
Simon Lovelock
5,537 Points

JQuery issue inArray()

Hi guys looking for help again...

Using the 'inArray' method check if the array contains the string 'Andrew'

================= My code:

var values = $(".required").map(function(){ return $(this).val(); });

return $.inArray("Andrew", values);

=================

Don't think i'm using inArray correctly but not sure.

Cheers,

Si

5 Answers

J.T. Gralka
J.T. Gralka
20,126 Points

Simon,

It looks like you're using the jQuery.inArray() function correctly; however, it seems as if you're misapplying the return keyword. It seems like that where you might be failing the code challenge. Because you're not writing a function of your own or filling out an anonymous function in this example, it's okay to just write:

$.inArray("Andrew", values);

for the second part of the code challenge. You're not writing a function here, so there's no need to return anything; in fact, you're not even printing out true or false. All you're doing is calling the function.

Give that a try (i.e., removing the return keyword before your method call) and let me know if you're still having trouble. It looks like you understand how to use the jQuery.map() and jQuery.inArray() methods well enough though, so I'd say you're well on your way to earning the Form Validation and Manipulation badge!

Good luck!

J.T.

J.T. Gralka
J.T. Gralka
20,126 Points

Milo,

We can utilize the $.inArray() method by passing in two arguments: the first is the key that we're searing for and the second is the collection we're searching in. The common analogy is "needle in a haystack":

$.inArray(needle, haystack);

...or, in other words:

$.inArray(key, array);

So, in this case our needle (key) is the string we're searching for -- "Andrew" -- and the haystack is the array containing all of the fields. values is the name of the array we created.

var values = new Array();
// Now use $.each() to populate the array.
$.inArray("Andrew", values);

If you're still feeling confused, check out the jQuery API for more information on how you can use the $.inArray() method in your jQuery applications.

I hope this helps to clear things up! :-)

Best,

J.T.

Simon Lovelock
Simon Lovelock
5,537 Points

Hi J.T.,

Thanks for your response, that solved it. Simply had to remove the "return" keyword.

I understood it to this point $.inArray("Andrew"); and have watched the video several times and gone back on the chapter but still for the life of can't figure out how you knew to put ', values' in there. Where did you learn that?

Thanks for the answer though it is much appreciated.

Hi J.T. Thanks for the in-depth explanation I appreciate you taking the time to do it.