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

Gareth Redfern
Gareth Redfern
36,217 Points

Help 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

Well first you have to call the "return" method.

try this at the end of the "containsBlank" function:

return $.inArray(true, blanks) != -1;
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi 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
Gareth Redfern
36,217 Points

Hi 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
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Your 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
Gareth Redfern
36,217 Points

Hi 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
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi 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
Alex Plaza
22,923 Points

Js is kind of a dead end to me. I wouldn't do it without the forum D: