Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build an Interactive Website Form Validation and Manipulation jQuery Utility Methods

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,825 Points

Using map utility method on code challenge

Hello Treehouse :)

I've been trying with a code challenge for a little while and I feel like I'm close but can't seem to find the answer.

This is the question.

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

My code

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

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

});

which gets this response.

Bummer! The values aren't what we're expecting. Please try again

From what I can see, the code is doing exactly what's being asked but I must be missing something. :-)

Maybe it's cause you cached the jQuery object $('.required') into a var $required. This is unnecessary in this case. Also, you seem to be trying to return some kind of boolean value out of the anonymous function located in .map(). How come?

I'm pretty sure the right answer would look like this:

var values = $('.required').map(function() {
  return $(this).val();
});
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,825 Points

It looks like I wasn't too far off then. I put the object in its variable as it seemed like the right thing to do, it seems to be common practice in Andrew's video's.

And since we're looking for blank values, this is why I tried to include a test for an empty string.

In fact, I've just removed the empty string and left the cached variable in and the challenge passes. All good.:-)

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jonathan;

Let's break this down.

1. Create a variable called values:

var values

2. Using jQuery select .required

var values = $(".required")

3. Call map on it

var values = $(".required").map()

4. In the map call write an anonymous function and return $(this).val() i.e. the value of the required field.

var values = $(".required").map (function () {

    return $(this).val();

});

I hope that makes some sense, if not post back and we'll try to get it sorted out.

Happy coding,

Ken

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,825 Points

Yup, turns out I was close, I just needed to remove the empty string! :-)