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 trialWesley Haines
4,934 PointscontainsBlanks 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.
4 Answers
Tom Mertz
15,254 PointsEverything 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
15,254 PointsWow 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...
Wesley Haines
4,934 PointsHey 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
15,254 PointsHi, 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
4,934 Pointsyea... 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
15,254 PointsYeah 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
15,254 PointsHere'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
4,934 Pointsawesome tom, you the best!