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

Mark Flavin
Mark Flavin
10,199 Points

jQuery Utility Method Quiz

I seem to be stuck on the first question. I am not sure what I am doing wrong as it appears my code should be fulfilling the objective.

Question: Call 'map' on the inputs with the class 'required' and return each of their values and store it in a variable named 'values'.

Answer:

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

19 Answers

After a lot of frustruation I just realized that if you make the mistake of answering the question inside a function like this:

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

it will return the stated error: "you did not set the var "values". The question does not require a function so it should only be:

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

Everyone else's answer is wrong. But your's isn't. Kudos

James Barnett
James Barnett
39,199 Points

Amanda -

I really don't understand this stuff.

I couldn't help but notice, you appear to have skipped over all of the foundational deep dive courses.

The Build an Interactive Website course comes at the end of a several course long sequence, which builds up to it.

I'd recommend you check out the Become a Web Designer learning adventure. The courses can of course be taken in any order, but following the order in the learning adventure is strongly recommended.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
return $.inArray(true, values);

You don't need to return since you're not in a function. This causes a syntax error causing the script not to be interpreted and that's why you get the "You haven't set the variable of 'values" error.

So:

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

Regards Andrew

lyonel scapino
lyonel scapino
14,191 Points

Hi Andrew, I believe the thing you use to have your students take challenges is quite flawed. I was so stuck I had to come here to find out about the right answer just to realize that my answer was perfectly fine, I mean absolutely identical except it was not only on one line, but in spite of such a small difference it was not processed correctly. I am quite sure it is also a challenge to have these code checkers up and running but believe me it s also quite frustrating to spend long minutes trying to figure out what s wrong just to realize that nothing really is, except the website that is charging you... Great course, great teacher besides that of course..

Here you go:

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

I just copied and pasted your answer and it doesn't work. I really don't understand this stuff.

Diana McClung
Diana McClung
1,685 Points

Might be worth mentioning that Andrew left "return" off of that code.

If you put "return" where it is meant to go...the code passes.

Okay, I tried the following:

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

and received the error: "Bummer! You haven't set the variable 'values'". But then again, I have not been able to get any of the code challenges in this section to work for me.

also, I keep seeing the code "$.inArray(true, values);" in this discussion. Why is this relevant to the first task?

i typed in the following - and got the green light!

var values = $('.required').map( function() {

return $(this).val()

});

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Mark,

You're almost there!

The .map() method documentation states:

Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.

All you need to do is return.

Regards Andrew

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Remove return from the last last line. Return is a keyword only used within a method or function. Having the return there is causing a syntax error so the first step in the challenge is failing. Remove it and see what happens then.

Hi Mark! Great question :D I'm going to ask the Team for help and feedback for you. Stay tuned.

Mark Flavin
Mark Flavin
10,199 Points

Doh! Thanks Andrew and Elizabeth :)

I keep getting "you haven't set the variable of 'values'"

can you please tell me what I am doing wrong:

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

In my case, when I try to run this code, it tells me "you did not set the var "values""

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

That is wrong?

James Nelson
James Nelson
23,956 Points

Using the inArray utility function could I pass in multiple string values? Say for example if we are looking for the string of 'Andrew' or 'andrew', would the following work?

return $.inArray("Andrew" || "andrew", value);

Jason Foster
Jason Foster
26,231 Points

I found this all a bit confusing. Guess I'll have to re-watch the video

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

I don't think so no. As JavaScript would interpret this "Andrew" || "andrew" as "Andrew". You'd need to do something like return $.inArray("andrew", value) || $.inArray("Andrew", value);.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Within the map anonymous function you must return the value.

If I recall $.inArray is used in a later step/task in the same challenge.