Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Wesley Haines
Wesley Haines
4,934 Points

containsBlanks checking values code challenge 3 of 3

the video really sped through introducing jquery like no other video i have seen on this site yet... so I really have been stuck on figuring this out. http://teamtreehouse.com/library/build-an-interactive-website/form-validation-and-manipulation/checking-values.

http://codepen.io/anon/pen/fJHqE

4 Answers

Tom Mertz
Tom Mertz
15,254 Points

Everything looks pretty good, but when you do your

return blanks.indexOf(true);

You are telling jQuery to search through your array and return the value (1, 2, 3, 4, 5....) of the 1st place it finds true If jQuery doesn't find true, then it will return -1.

But with your containsBlanks method you want to have it return either true or false, so you'll need an expression to evaluate the 1, 2, 3, 4 or -1 that the indexOf method gives you. In this case checking to see if the returned value is equal (==) to -1 will mean there is not a blank in your required fields, and it will return true. But you want the method to return true when there is a blank, so you'll need to use !=

return blanks.indexOf(true) != -1
Tom Mertz
Tom Mertz
15,254 Points

Wow sorry, the code mark up is really not working and the page won't let me edit my post at all. I included the finished code in CodePen for you to check out. Hopefully it works...

http://codepen.io/anon/pen/rElKi

Wesley Haines
Wesley Haines
4,934 Points

Hey Tom,

Thanks for the help. I added the "!= -1" to the end of the return call, but it still does not seem to work.

Is there another hack on this problem that you (or anyone else out there) knows of?

Still not sure what I am doing wrong...

Tom Mertz
Tom Mertz
15,254 Points

Hi, it definitely works. I was a bit hasty when I said that the rest of your code looked good it seems ;)

This chuck of your code is the problem

$required.each(function containBlanks()

{

var blanks = new Array(); blanks.push($(this).val() == "");

});

You want to start off the code with the

function containsBlanks() {

THEN do you .each() function on required with it's new anonymous function. See if you can get that working

Wesley Haines
Wesley Haines
4,934 Points

yea... I do not know what you are really saying in that last comment. "THEN do you .each() function on required with it's new anonymous function."

Would you mind posting the answer? I cannot seem to figure out where to place the ".each()" method

$required = $(".required");
function containBlanks()

{

  $required.each(function () {

  var blanks = new Array();
  blanks.push($(this).val() == "");

  };

});

return blanks.indexOf(true) != -1
}
Tom Mertz
Tom Mertz
15,254 Points

Yeah you almost got it, you just need to make sure the array is defined outside (above) of your each function and that you return your array inside your containsBlanks function (up one curly) then end it with a ; and you should be good :)

Tom Mertz
Tom Mertz
15,254 Points

Here's what worked for me

function containsBlanks()

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

Make sure you are putting "contains" if that's what it asks, I see that your code does have "contain" instead.

Wesley Haines
Wesley Haines
4,934 Points

awesome tom, you the best!