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

Joakim Skansen Flatmoen
PLUS
Joakim Skansen Flatmoen
Courses Plus Student 24,223 Points

Create 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
samiff
31,206 Points

After 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;
}

Sam. your explanation is awesome.. very crystal clear. You should be doing videos to teach us :) Thanks a lot

Larry Kasaija
Larry Kasaija
3,081 Points

I appreciate the help Sam. I hope some day I can explain code like you.

Thanks sam. I'm not sure if the video lecture prepared me to answer this question.

thanks, well done

Thank 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

Agree. 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.

function requiredValues(){ var myArray = new Array; $(".required").each(function(){ myArray.push($(this).val()); }); return myArray; };

Thank 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
Yisheng Qian
5,800 Points

@Sam. This is very helpful! The video is quite hard to understand.

Gil 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.

@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.

Thank you

Brian Pelowski
Brian Pelowski
7,202 Points

This was very difficult. I feel like the question does not even accurately ask what they are wanting us to do...

Alice Mello
Alice Mello
1,591 Points

I'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
Matt Trask
10,027 Points

you 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
Gil David
8,811 Points

I 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
Nikolai Montesclaros
11,928 Points

Gil 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.

Thanks 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.