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

Form Validation and Manipulation > Checking Values

I've been trying to wrap my head around this one for a few days, but no matter how many breaks I take I just haven't been able to find the answer. I'm missing something.

The question is: "Create a method called 'containsBlanks' that returns true if any inputs with class 'required' has an empty string in, false if not."

An explanation would be rather helpful.

22 Answers

samiff
samiff
31,206 Points

This isn't the prettiest way to do it, but I think it helps illustrate what you want to happen. "myArray" is the returned array from requiredValues() that holds all the values of the inputs with the class "required". We simply need to iterate over the array to check and see if any of those indexes contain an empty string. If any array index contains an empty string, return true. If there are no empty strings in the array, then containsBlanks() will return false.

var containsBlanks = function()
{
     var myArray = requiredValues();

     for(var i=0; i < myArray.length; i++)
     {
          if(myArray[i] === "")
          return true;
     }

     return false;
};
R Brewster
R Brewster
15,328 Points

This seemed to work.

function containsBlanks(){
  var valuesArray = new Array();
  $('.required').each(function()
    {
    valuesArray.push($(this).val() == "");
 });
  return valuesArray.sort().pop();
};
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Want to paste where you're at?

The steps are:

  1. Create function containsBlanks
  2. Create an array in the function
  3. Cycle over ".required"
  4. Push if the input is empty on to the array
  5. Return if the array has any trues in (the video suggests one method)

Regards Andrew

samiff
samiff
31,206 Points

Hey Jordan, back in your requiredValues() function, you're adding to an array all the values of inputs with the "required" class, then returning that array. So when writing your containsBlanks() methods, you can call requiredValues() to generate that array for you.

var myArray = containsBlanks();

After that, all you need to do is look for an empty string or "blank" in that array. You can iterate over the array in a variety of ways, including using each, or just stick with a more traditional approach. Hope that helps!

Thank you for both of your replies, Andrew and Sam.

Andrew, this is what I have for the first two sections of the challenge. After that is where I get lost:

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

  var requiredValues = function() {

         var myArray = new Array();
       $('.required').each(function()

       {
            myArray.push($(this).val());
       });

         return myArray;
  }

Sam, your explanation I feel helped me understand a little more, but things still don't feel quite right.

Hi Jordan, Did you have it done finally the way Andrew is explaining? If so,, can share the code .. need to read it and understand it. I'm still lost.

I know Sam also present another solution but I see that harder to understand.

Thanks so much. and I hope you are doing great.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The next step is to create a function called containsBlanks method like the video. Here's where you'd start. It looks very similar to the requiredValues method.

function containsBlanks() {
   var myArray = new Array()
   $('.required').each(function()

    {
        myArray.push($(this).val()); // Add the comparison to the empty string here not just the value
   }
   );


   return //Use the strategy shown in the video to return true or false
}
samiff
samiff
31,206 Points

@Andrew why would you want to create a new array and repopulate it for containsBlanks()? What purpose would requiredValues() serve then?

Thanks to the help I got through it, but having a grasp on the fundamentals is important, so I'll have to be sure to make my way back and do it again. I'm not sure how anybody does it, no programming language has ever given me a headache as big as jQuery has. Haha, you're both insane.

samiff
samiff
31,206 Points

Understanding "what" should happen is way more important than understanding "how" to make it happen here. The "how" is easily gained through repetition eventually.

I do have an understanding of what needs to happen, but I find myself getting lost among the structure of things. It's possible I'm quite simply not patient enough, though I do try.

Cheers Sam, and Andrew. Appreciate the help!

samiff
samiff
31,206 Points

I'm just as lost as you honestly :stuck_out_tongue: If you see yourself using more jQuery in the future, I definitely recommend Pro jQuery from Apress. It's not at all a "pro's" book. I'm currently reading it and it's made a lot of things click into place for me. Once you start playing with all the provided example code, and cross-reference the API site, it becomes easier to grasp I think.

Jeremy Hogan
Jeremy Hogan
15,451 Points

Thank you! I should've figured it was my syntax. I was missing the last ")" and semi-colon.

/* ignore this comment. Posted to the wrong thread */

Nick Toye
Nick Toye
5,985 Points

OK I'm confused massively here.

What on earth is this containsBlanks??? and where is the function example for what the challenge is asking? requiredValues() ???

I understand that there is an awful lot of assumptions with JavaScript, but the replies in this thread just make no sense to me at all.

This is what I have.

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

Doesn't work. :(

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Nick,

containsBlanks is for the next step in the code challenge.

In this step you need to return the array of required values. In your case it's the array called myArray. If you don't return anything thing the challenge will fail. Well done for getting this far!

Nick Toye
Nick Toye
5,985 Points

Right, well I'm working on client work now, so when I get back on it I will try again. I may come back with more questions.

I did this other way, and it works.

var required = $(".required");

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

== 1 means there's a true in the 2nd position in the array. What happens if there's a blank in the 3rd position.

$.inArray returns -1 when it doesn't contain any since -1 isn't a valid position in an array. You should test if the result of $.inArray is not -1 or greater than -1.

i'm stuck on this too i understand this up to the 3rd one where you have to put the content black in and i have know idea where i should start?

here is what i have so far!

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

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

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

function containsBlanks(){

};

some one help!!

sorry all the code is there but when ever i copy it does not format the same way that it was pasted!!

Help Andrew

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Aaron,

Here's some comments on how to structure your code.

function containsBlanks(){
    //Create an array 'values'
    //Cycle through each the '.required' input
      //In the each push the comparison of $(this).val() == "" where "" is an empty string

    //return if the 'values' array has a true value in it
};

This way of doing it is in the pervious video.

Hope this helps.

Hello I have having a interesting time for a few reason that I will share, here was my answer that worked:

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

I made to mistake in here that I had to figure out one was forgetting to close the inner function:

        {
            blankField.push($(this).val() == "");
            }  // here I have only closed the function without thinking about what it was held in
//then fixed it to:
        {
            blankField.push($(this).val() == "");
            });

My other mistake that I only saw after review the video was:

    return blankField.sort().pop();  // I had blankField.pop();then thought I silly me to blankField.push().pop();
//  then remembered it needed to be sorted first then popped.    
}