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 trialGareth Redfern
36,217 PointsHelp With Checking Values Task jQuery
I am trying to work my way through the checking values code task and could do with some help. The question is:
Create a method called 'containsBlanks' that returns true if the array has any blanks in it, false if not.
I have created the containsBlanks function which creates an array with the values of true or false depending on whether there are any blanks:
// get all the inputs with the class of required
var $required = $('.required');
function containsBlanks() {
var blanks = new Array();
$required.each(function() {
blanks.push($(this).val() == "");
}
}
My question is how do I return true or false depending on whether there are blanks or not.
Thanks Gareth
7 Answers
William Moss
8,710 PointsWell first you have to call the "return" method.
try this at the end of the "containsBlank" function:
return $.inArray(true, blanks) != -1;
Andrew Chalkley
Treehouse Guest TeacherHi Gareth,
Just like William says to you need to use the return keyword at the end of the function.
You can use something like inArray to do it.
Let us know if you have any more issues.
Regards Andrew
Gareth Redfern
36,217 PointsHi William/Andrew,
Thank you for the help, I haven't come across the inArray method before. O.k. so I have used the code that William suggested but now I get a "It looks like Task 1 is no longer passing" error. Any ideas why that would be? My full code is:
function isValidEmail(email) {
return email.indexOf("@") != -1;
}
var $required = $('.required');
function requiredValues() {
var $values = new Array();
$required.each(function() {
$values.push($(this).val());
});
return $values;
}
function containsBlanks() {
var blanks = new Array();
$required.each(function() {
blanks.push($(this).val() == "");
}
return $.inArray(true, blanks) != -1;
}
Andrew Chalkley
Treehouse Guest TeacherYour right w.r.t. inArray. I go in to inArray in a later video.
I checked in the video and at this point I showed you how to, instead of the indexOf for the array, use a sort().pop() strategy.
You're missing a ); in the end of the each.
$required.each(function() { blanks.push($(this).val() == ""); }
And that should pass :)
Gareth Redfern
36,217 PointsHi Andrew, yes that worked a treat thank you.
I tried to work through the example and use a sort().pop() strategy but I couldn't figure out how to return true or false depending on whether there were blanks. Could you show an example with this so that I have both methods?
Thank you for all your help ;-)
Andrew Chalkley
Treehouse Guest TeacherHi Gareth,
When you use the keyword return the result of the following expression is returned.
return blanks;
Would return the array. e.g. [true, true, false]
return blanks.sort();
Would return an array with false_s at the beginning and _true_s. e.g. _[false, true, true]
return blanks.sort().pop();
Would pop off the last value at the end. If there are true_s _true would be returned.
Regards Andrew
Alex Plaza
22,923 PointsJs is kind of a dead end to me. I wouldn't do it without the forum D: