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

jQuery Checking Values 3/3 Code Challenge

Throwing in the towel and asking for help on this one.

This is what is being asked: "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 so far:

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

I'm creating the method, then selecting all inputs with the required class, running a function over each of those, checking if any of the 'this'es have an empty string, and return true if it does, and false if it doesn't. Not sure what I'm missing, almost every error that comes up says that task 1 is wrong when this has nothing to do with that.

4 Answers

Do this:

var required = $(".required");

function containsBlanks() {
 var blanks = required.map(function(){
   return $(this).val() == "";
 });
 if($.inArray(true, blanks) == 1) { return true; }
 else { return false; }
}

You rock, that did the trick. Thanks a bunch! Will probably revisit this section.

Your welcome.

The jquery stuff always triped me up...It took me a bit to figure this one out. I'm looking for some Javscript courses elsewhere.

You have to put parentheses around your conditional. Like this:

 if ($(this).val() == "") {

Ugh, can't believe I missed that.

However, that still isn't working. Any ideas, anyone?

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

If one had not gone on into the next segment to learn the more optimized jquery method the following worked for me.

var $required = $("required");

function containsBlanks () {
  var blanks = new Array();
  $required.each(function(){
     blanks.push($(this).val() == "");
  });
  return blanks.indexOf(true) != -1;
}

It's missing dot(.) for required because it's a class.

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