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 trialMark Flavin
10,199 PointsjQuery 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
Evans Rutoh
3,498 PointsAfter 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()});
James Barnett
39,199 PointsAmanda -
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
Treehouse Guest Teacherreturn $.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
14,191 PointsHi 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..
Alvin Brooks
2,201 PointsHere you go:
var values = $('.required').map(function(){ return $(this).val(); });
Amanda Swartz
2,098 PointsI just copied and pasted your answer and it doesn't work. I really don't understand this stuff.
Diana McClung
1,685 PointsMight be worth mentioning that Andrew left "return" off of that code.
If you put "return" where it is meant to go...the code passes.
Brent McCarthy
2,736 PointsOkay, 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?
Bola Ajayi
5,145 Pointsi typed in the following - and got the green light!
var values = $('.required').map( function() {
return $(this).val()
});
Andrew Chalkley
Treehouse Guest TeacherHi 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
Treehouse Guest TeacherRemove 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.
ecp
838 PointsHi Mark! Great question :D I'm going to ask the Team for help and feedback for you. Stay tuned.
Mark Flavin
10,199 PointsDoh! Thanks Andrew and Elizabeth :)
Kathleen Fitzgerald
3,026 PointsI 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);
Niels Siskens
6,661 PointsIn 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
23,956 PointsUsing 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
26,231 PointsI found this all a bit confusing. Guess I'll have to re-watch the video
Andrew Chalkley
Treehouse Guest TeacherNo worries Mark!
Andrew Chalkley
Treehouse Guest TeacherI 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
Treehouse Guest TeacherWithin the map
anonymous function you must return
the value.
If I recall $.inArray
is used in a later step/task in the same challenge.
Elton Zhang - Healthily
5,362 PointsElton Zhang - Healthily
5,362 PointsEveryone else's answer is wrong. But your's isn't. Kudos