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

Marie Veverka
Marie Veverka
12,117 Points

Checking values task 2

Hi there, I have been having problems with this chapter all day. I finally got task 1 working, and now after attempting task 2 it says task 1 no longer works. After trying several things, I finally just cut and pasted the code from the lesson here:

function isValidEmail(email){
  return email.indexOf('@') !== -1;
}
isValidEmail();

function requiredValues() {
  var blanks = new Array();
  $required.each(function() {
    blanks.push($(this).val() == "";
  });
  return blanks.sort().pop();
  }

I am sure this isn't right either, but I have no idea what to even try anymore.

3 Answers

You need to create a new Array (which will hold the values of the elements with a class of "required"). Then you iterate over each element with a function which pushes the values onto your new array.

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

First of all, you don't need to invoke any of the functions that you have to write, so you can get rid of the line that says: isValidEmail();.

Second, read the task again. You need the values of all the inputs with the class .required. It doesn't matter if those values are empty or not.

You don't need to sort them, or pop anything, either.

And finally, in your code $required will throw a Reference Error. It's a variable that hasn't been defined. You might want to replace it with a $('.required') selector, instead.

Marie Veverka
Marie Veverka
12,117 Points

Thanks Jeff! Also, thanks Dino as well. For some reason I hadn't gotten task 1 to work until I added the isValidEmail(); piece. Then, when I deleted it as you suggested it was fine. That may have been my original problem with task 2 all along. I knew the code I had above wasn't right, I had just gotten to the point where I had no idea what to do anymore so I copied something in from the lesson in the video - which also still doesn't work when I put it in the browser. Go figure.