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

Form Validation and Manipulation- Checking Values

Stuck on code challenge 2!

It's asking me to "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'."

I have entered:

var $requiredValues = $(".values");

function requiredValues(values){ var values = new Array(); return blanks.push($(this).val() =="");

};

What am I doing wrong??

4 Answers

Hey Dave, not sure if you ever managed to solve this, but maybe I can help. You're on the right track but I think maybe mixing a few things up. I never saw and/or heard an explanation on how to solve this either, so it took me a while to get through.

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

Hi Dave,

You're probably having issues for a few reasons:

  1. It looks like you're confusing the reason defining a formal parameter for a function, and
  2. it also looks like you're not populating your values array using the $.each() method in jQuery.
  3. You're not referencing variables consistently by name and you're not returning the complete array properly.

First, take a look at the way you defined your requiredValues function:

function requiredValues(values){ // Why are you passing values into this function?
    var values = new Array();
    return blanks.push($(this).val() ==""); // What is blanks?
};

The comment I've added on the first line of this function definition points out that you've included a formal parameter to your function signature. However, it doesn't appear that you ever use that parameter anywhere in your function body; as a matter of fact, you override the scope of that parameter when you define an array values on the next line. Consider striking the parameter from your function definition. Also, consider what collection you're pushing your values onto; is it really named blanks? Isn't it named values?

Secondly, you also seem forget to add the values to the values using the $.each() method in jQuery. You should rewatch Andrew's video when he talks about how to do this, but as an example, imagine I wanted to select all values from elements with a class of contact_info and add them to an array. I might do that like this:

function getContactInfo() {
    var arr = new Array();
    $(".contact_info").each(function(){
        // How would I add the values to the array?
    });

    return arr;
}

You'll notice I left the content of my anonymous function blank, but if you go back and practice using the $.each() method, I think you'd be able to figure out what you would need to do add the values of .contact_info to the array. Keep in mind the above example is analogous to how you would go about solving this code challenge.

The last thing I might add is that it seems like your version of the requiredFields function tries to return the individual values pushed onto some sort of array; instead, you should be returning the entire array of values, but only after you have populated the array with the required values using the $.each() method.

Hopefully my explanation and example gave you some insight into solving your issues with this code challenge. My advice would be to go back and rewatch the videos for this stage of the badge after you've contemplated the points I brought up in this post. If you find that you are still having trouble, don't hesitate to let me or the Treehouse support team know (via email at help@teamtreehouse.com). We'd be more than happy to elaborate on this explanation.

Best of luck,

J.T.

Hello J.T.

Thank you very much for you detailed response to my question. Unfortunately, I'm still having trouble with this challenge.

In re watching several of the videos, I'm not seeing where he shows us how to return an array, but how to determine if there are values in the array.

Let me know if I understand this correctly.

First, I need to set my variable "requiredValues". After this, I need to insert the values into an array. I do this using the "each" function. I have to do this in order to return the array of values.

I know I add values to an array by using "$.each()"

I apologize for the confusion. I'm new to jquery, and trying to grasp it the best I can.

Thanks again for your help,

Dave

@Jordan,

Thanks for the reply, and yes, I did manage to solve, though not without some help.

I was helped through the equation by the Treehouse support team, and believe it or not, the answer they gave me didn't actually contain the "new Array" sequence at all.

I'm sure that there are a few different methods to complete a challenge, but this is one so far that really had me stuck for a bit.

I ended up going through the stage a few times, just to make sure I had it down. In the end, they recommended that I call jquery's ".map(function)(){}" in order to solve, something that the tutorials didn't get into until the next video.