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 Build an Interactive Website Form Validation and Manipulation Checking Values

Louis Sankey
Louis Sankey
22,595 Points

Returning an array in JavaScript

I am having trouble with one of the challenge questions in the JavaScript form validation section. It says "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'.

So I have tried creating a variable:

var $required = $(".required");

 //here first of all I don't know if I should be going $(".required input")//

//Then I tried creating a new array within the method//

function requiredValues(){
var values = new Array();

//here I already don't know if I'm on the right track, but it gets even more fuzzy//
//I try calling 'each' on the variable//

$required.each(function){
values($(this));
});

//I know I need to return something//

return values;
}

Sorry if my syntax here sucks. I know I can get the syntax right, I just need some general direction to put me on the right track. Thank you!

Louis Sankey
Louis Sankey
22,595 Points

also a tip on how to get the markdown to work would be helpful. Thank you.

Sarah Bradberry
Sarah Bradberry
7,115 Points

Markdown is done by putting three ` marks at the beginning and end of your code.

It's the symbol at the bottom of the tilde key ~

After the three opening ` marks, write the type of code you're showing : HTML, javascript etc.

2 Answers

You've to push the values:

function requiredValues() {
    var values = new Array();
    $(".required").each(function() {
        values.push($(this).val());
    });
    return values;
}
Louis Sankey
Louis Sankey
22,595 Points

Thank you both for your help! Its much appreciated!