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 trialJoakim Skansen Flatmoen
Courses Plus Student 24,223 PointsCreate a method called 'requiredValues' that returns an array of all the values
Can someone help me with this code challange?
"Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'."
11 Answers
samiff
31,206 PointsAfter you've declared the function, you have to create a new array, then using a jQuery selector, you target all inputs with the "required" class name. Each of those elements are now part of a jQuery object, and we can use the each method to iterate over every element and execute a function on it.
What you want to do inside that function is add the value of the current element to the created array. An easy way to do that is by pushing the value onto the array. You can access the value by calling .val() on the current element, which is easily referenced using "$this". Don't forget to return your array.
var requiredValues = function()
{
var myArray = new Array();
$('.required').each(function()
{
myArray.push($(this).val());
});
return myArray;
}
Collin Graves
2,140 PointsThank you, Sam! I finally had to copy and paste this question in to Google. I've watched the video about 17 times, but I seemingly met my match with this teaser.
Thanks again!
Yisheng Qian
5,800 Points@Sam. This is very helpful! The video is quite hard to understand.
nik
8,925 PointsGil David It's because they only want you to select the inputs with the class of '.required'. If there were any other input elements in the code, it will not be affected. Now if the '.required' class was used on other elements other than inputs then we would have a problem. But in this exercise it's pretty straight forward.
nik
8,925 Points@Sam thanks very much for the explanation. I watched the video so many times but for some reason after reading your post it finally clicked. I kept adding a value equal to blank instead of just leaving it alone. I guess I was basing it too much off the vid.
Michael Ballentine
9,932 PointsThank you
Brian Pelowski
7,202 PointsThis was very difficult. I feel like the question does not even accurately ask what they are wanting us to do...
Alice Mello
1,591 PointsI'm having the same difficulty, the video explains very well what is being typed but I don't get the logic, why do that... Is the Jquery course for beginners (like me) who have no idea what is an array, a function, an each method, etc... ? Or is there another video we should watch first and only then the Jquery?
By the way, I love the treehouse videos and method, really helpful!!
Matt Trask
10,027 Pointsyou should watch intro to programming, then javascript then jquery. jQuery is a Javascript framework, so with no knowledge of Javascript, you will be lost in jQuery.
Gil David
8,811 PointsI just don't understand one thing. when you choose the required class with the Jquery Selector $('.required') you should have selected the inputs too like this $('.required input')?
Nikolai Montesclaros
11,928 PointsGil won't that statement go one level below? Meaning that what will be selected is an input nested under a required class. This to me is different from selecting an input with class required.
Andrew Richards
9,771 PointsThanks for the explanation. The jQuery videos aren't helpful at all. They don't explain the underlying logic. I'm going to do the CodeCademy course and then return to finish this exercise.
Sarah Masare
Courses Plus Student 6,506 PointsSarah Masare
Courses Plus Student 6,506 PointsThank you for this explanation!
Carlos Diaz
Courses Plus Student 9,006 PointsCarlos Diaz
Courses Plus Student 9,006 PointsSam. your explanation is awesome.. very crystal clear. You should be doing videos to teach us :) Thanks a lot
Larry Kasaija
3,081 PointsLarry Kasaija
3,081 PointsI appreciate the help Sam. I hope some day I can explain code like you.
Cliff Garibay
7,023 PointsCliff Garibay
7,023 PointsThanks sam. I'm not sure if the video lecture prepared me to answer this question.
Scott Phelan
2,934 PointsScott Phelan
2,934 Pointsthanks, well done
Michael Ballentine
9,932 PointsMichael Ballentine
9,932 PointsThank you! much better explanation than the video..no offense at all to Andrew, he just doesn't explain things enough, he just types it out, which is bad because I keep getting lost in this jQuery island
Carlos Diaz
Courses Plus Student 9,006 PointsCarlos Diaz
Courses Plus Student 9,006 PointsAgree. I also completely get lost. No Offense to Andrew neither, but he just type out and does not explain with more detail the logic behind each statement. So first I have to watch the video few times and most of the time I have to google the answer too to see if you guys are having the same issue and if someone posted the same question. I know that Jquery is more complex than html but the html teacher explain very well what is behind any html declaration.. have a good one.
Richard Glover
10,068 PointsRichard Glover
10,068 Pointsfunction requiredValues(){ var myArray = new Array; $(".required").each(function(){ myArray.push($(this).val()); }); return myArray; };