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

Building Interactive Website - Form Validation - jQuery Utility Methods

On the final challenge part 1:

"Call 'map' on the inputs with the class 'required' and return each of their values and store it in a variable named 'values'."

No matter which way I structure my code, no matter how many times I watch the video, no matter how many resources I read through I still do not understand how to complete this part of the challenge.

The .map() function makes sense but I keep getting the message You haven't set the variable 'variables'

Help?

9 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Want to paste in your code and then we can take a look?

$("input").each(function() {
    var values = $("input, .required", this).map(function() {
        return $(this).val();
    })
        return $.inArray values();
}
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

OK so there's a few things.

You don't need to go over each of the inputs like that. When you call map on $(".required") it will loop over them.

Because var values is within the each loop it's only available in there so that's why the code challenge isn't seeing your variable. So once you get rid of the each() call that should fix that.

Also I don't think the inArray is needed just yet but the inArray method takes two parameters. The documentation can be found here.

So what you're saying is something more like this:

var $values = values; //that being the variable outside the function
function returnValues () {
    var values = $(.required).map(function() {
        return $(this).val();
    })
        return $values;
}
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
var $values = values; //that being the variable outside the function

function returnValues () {
    var values = $(.required).map(function() { //keep
        return $(this).val(); //keep
    }) //keep
         return $values;
}

You're almost there. I believe the question doesn't require a function. You have done what the question requires inside the function. You don't need another $values variable. I've marked the lines you can keep.

Also one last thing you're missing double quotes from around the .required class.

Brilliant I had actually gotten as far but wasn't using quotes for the .required and that was where I was going wrong!!

Thanks Andrew

Since this conversation is fresh, I'll ask here instead of starting a new thread.

I'm attempting this without a function, but getting "You haven't set the variable 'values'."

var values = $(".required").map($(this));
  return $(this).val();
  return values;

I added a function to the .map() and it worked. Silliness.

var values = $(".required").map(function(){
  return $(this).val();
  return values;
});
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You can get rid of the return values;

var values = $(".required").map(function(){
  return $(this).val();
});

Should be enough.