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 trialUnsubscribed User
22,441 PointsForm Validation and Manupulation Challenge Task 2 - Help!
Hi, i'm having problems writing the followong 2 task of the code challenge of Jquery.
Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.
Can anyone help me please?
10 Answers
Andrew Kiernan
26,892 PointsHi Juan Pablo!
You can take a look at the code below. I added comments so you can see what each line does. Let me know if you have any questions!
-Andrew
function requiredValues() {
//Get all of the .required tags
var $required = $(".required");
//Set array for return values
var array = [];
//Loop through .required tags
$required.each( function () {
//Add the values of each .required tag to the array
array.push(this.value)
})
return array
}
Steve Leichman
7,008 PointsThis is the most difficult challenge I've encountered. Between the speed of the videos and the complicated instructions, this is taking me ages to get through. My frustration levels are through the roof.
Steve Leichman
7,008 PointsAndrew K's answer did it for me. This was mostly infuriating because this topic wasn't covered in this video and hadn't been covered in ages.
Unsubscribed User
22,441 PointsYeah! I wasn't taking the each method in account! Thanks...
In the third task they ask me: Create a method called 'containsBlanks' that returns true if any inputs with class 'required' has an empty string in, false if not.
I'm typing:
function containsBlanks() { var $required = $(".required"); $required.each(function() { $(this).val() == ""; }) }
But it doesn't work...
Thanks a lot for your help man!
Andrew Kiernan
26,892 PointsI can't seem to get the code challenge to work for me to test it myself, but I believe you want to combine the ideas behind the isValidEmail and requiredValues methods. So you want to test each $required element and see if it has an index value for the empty string. If it does then push 'true' into an array and 'false' if not. Then you can use jQuery's sort() method (which sorts all false values first, then true values), and jQuery's pop() method to return the last value, which will be true if there was an input that contained blanks, and false if none of the inputs contained blanks. So something like
function containsBlanks () {
var $required = $(".required"),
array = [];
$required.each(function() {
if (this.indexOf(" ") !== -1) {
array.push(true)
}
array.push(false)
})
return array.sort().pop()
}
Unsubscribed User
22,441 PointsThanks man! Lot of help!!! Not working though :s
SL Starr
17,543 PointsI've struggled to the point of reviewing the entire library of JS again trying to shake this challenge. Still can't get it! Tough!
Mike Rolih
9,766 PointsSteve Leichman I completely agree. I spent a ton of time on this one and finally got it after seeing Andrew's response. This entire course set needs to be redone IMHO.
louiecamacho
10,980 PointsYup, I am stuck and have been for a month now and I'm the kind of person who doesn't like to skip sections. So until I solve this, I can't allow myself to watch more lessons. HELP!
SL Starr
17,543 Pointsfunction requiredValues() {
var $required = $(".required");
var array = [];
$required.each( function () {
array.push($(this).val())
})
return array;
}
This is basically the same as Andrew K's and it will pass. Hang in there, Chalkley is super fast and difficult to follow, most of us will have to seek out some other basic learning to be able to comprehend js enough to follow jquery at this level. Google is your friend. I am reading eloquentjavascript.net right now to try and learn the foundations not covered in detail here.