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

HTML Build an Interactive Website Form Validation and Manipulation Checking Values

Pierre Poujade
Pierre Poujade
7,085 Points

Can't pass challenge 3/3

Hi,

I can't seem to figure this out: "Create a method called 'containsBlanks' that returns true if any inputs with class 'required' has an empty string in, false if not."

This is what I have:

function containsBlanks() {
  $('.required').each(function() {
    if ( $(this).val() == '') {
      return true;
    } else {
      return false;
    }
  });
}

What am I missing?

Pierre Poujade
Pierre Poujade
7,085 Points

I also tried this, I thought maybe it was expecting me to re-use the previously created function:

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

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

function containsBlanks() {
  var values = requiredValues();
  values.each( function() {
    if( $(this).pop() == '' ) {
      return true;
    } else {
      return false;
    }
  });
}

But it didn't pass either.

3 Answers

I'm gona be honest I can't figure out how to do it your way. I ended up doing it the way andrew did it and it worked. But I can tell you a few things. First of all notice the .each method. See how in the function requiredValues it's being used there too? Well you are using it differently even though you may not think so. See the $ symbol with () is actually an object. $ is short for JQuery you can use them interchangably. So you could have said JQuery('.required').each but the $ is shorter and I like it better. The important thing about this is that the ".each" method is a method of the JQuery object NOT a native method for arrays. So when you say value.each javascript has no idea what you are talking about because value is not a JQuery object. Instead say $(".required").each or JQuery(".required") those would be JQuery objects and so you can then use the "each" method on the object.

Second thing your return statements are messed up. The way you have it set up is if I put just my name in the first field and leave the rest blank it's going to return false (because the first field is not blank. So that would be correct its false.) the problem is that you haven't checked any other values yet. You need to check ALL the values first and then after that you need to decide to return true or false.

For some reason I was trying $(this).val() == "" and the result of the expression wasn't what I thought it should be so I'm not sure what's up with that. Go back and watch his video though he will teach you another way of doing this which is actually better and more cross browser compatible. However keep in mind that $ and JQuery is the same thing.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Excellent break down David Eichel.

Pierre Poujade does that make sense?

Also if you ever seen "Bummer: null" as an error (Which isn't the greatest. Our devs a working on it) it means that there's a syntax error in your JavaScript. So calling .each on a regular Array will cause it to have a syntax error and with result in a "Bummer: null".

Returns true if any INPUTS with the class REQUIRED. So $('input.required').each(..... Try that and see if it works. I don't see anything else wrong...

Returns true if any INPUTS with the class REQUIRED. So $('input.required').each(..... Try that and see if it works. I don't see anything else wrong...

Pierre Poujade
Pierre Poujade
7,085 Points

It doesn't pass either