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
Sebastian Wilson
15,710 PointsCode Challenge: Checking Values Task 2/3
Hello I am currently trying to
Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.
I have tried several times yet I haven't managed to do it correctly.
My current attempt below
function requiredValues(required) {
var a = new Array();
required.each(function(){ a.push($(this).val() == ""); }) return a.sort().pop(); }
8 Answers
Keith Wyland
10,576 PointsHi Sebastian, I believe the task is asking you to use jQuery to select the inputs with a class of "required", then put each of their values into a returned array.
Try starting with
$('.required')
Sebastian Wilson
15,710 PointsI had another go but I am still not getting there
Keith Wyland
10,576 PointsDo you have more code than just what you've pasted here? For instance, is there a variable that you've called "required" that contains something?
Sebastian Wilson
15,710 PointsHello here is what I have done.
function isValidEmail (email){
return email.indexOf("@") != -1;
}
$required = $(".required");
function requiredValues(){
var a = new Array();
$required.each(function(){
a.push($(this).val() == ""); }); return a.sort().pop(); }
Keith Wyland
10,576 PointsOk, I missed this before. The following line is where it's getting tripped up.
a.push($(this).val() == "");
You're pushing a true or false value into the array. That part is coming up in the next task of the challenge. This task wants the actual values of the inputs (what's inside the text fields.) And then return the whole array, not just the last value after sorting.
Does that help?
Sebastian Wilson
15,710 PointsThank you very much Keith for your help and time, that was the line that has been causing me issues.
Andrew Chalkley
Treehouse Guest TeacherThanks for chipping in Keith!
Keith Wyland
10,576 PointsHey, no problem!