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

Carlos Perez
Carlos Perez
5,939 Points

I'm having a problem with challenge #2 in the Form Validation and Manipulation stage of Building an Interactive Website

This is the question "Create a method called 'requiredValues' that returns an array of all the values of inputs with the class of 'required'."

This is my code

var $required = $(".required")

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

What am I doing wrong? Please someone answer.

Never mind =) got it to work. As it turns out I wasn't returning a value.

var $required = $(".required")

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

hey carlos I had the same issue. Looks like we are the same one, if you want to skype contact me on one of my profile links, didn't see any on yours

Carlos Perez
Carlos Perez
5,939 Points

Hey Tony, yea I was stuck on this challenge for a pretty good time until I realized I wasn't returning anything with the function. Skyping sounds like a plan, will do buddy. =)

Yep that's how we learn. I just realized the profile doesn't have skype so it's @worldwidewiz, I also added my github account - might be the easiest to share/comment on code that way

2 Answers

Hey I just got back to that task and am on the next one. I'm still getting denied on the map task - any tips?

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

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

// second try...
function valueStore() {
    var values = $required.map(function() {
        return $(this).val();
    });
  return values;
};

// keep getting error on both -  You haven't set the variable 'values'
Carlos Perez
Carlos Perez
5,939 Points

Hey Tony,

Your problem is you're not returning what the value should be equal to with the .val() method. You have to check if the input element contains a blank, one of the ways to check this is for an empty string "" Other than that your code seems good. =)