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

Simon Lovelock
Simon Lovelock
5,537 Points

JQuery Challenge

Another JQuery challenge needing help on.

Create a method called 'containsBlanks' that returns true if an array of required values has any blanks in it, false if not.

========================================== My code:

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

function requiredValues(){ var $inputs = $(".required");
var items = new Array();

$inputs.each(function(){ items.push($(this).val()); });

return items;
}

function containsBlanks(){

}

==========================================

I've included the code from the previous parts of the challenge as i think that some of it may be required to complete this one.

Any help is greatly appreciated. Not really sure where to start with this one. As you can see i have defined the function but not really sure what to put in it.

Thanks,

Simon.

5 Answers

samiff
samiff
31,206 Points

You're second function creates an array with values of the required fields. If any of those array positions are empty (blanks), you want to return true. If there aren't any blanks in the array, return false.

So there are quite a few ways to do this, but I'll point out that you really should be calling your second function (requiredValues) to create the array. After you have an array to work on, you can use different methods to see if any of the positions have blanks.

Simon Lovelock
Simon Lovelock
5,537 Points

Hi Sam,

I've updated my code and i know it isn't correct but hopefully it gives you an idea of what i am trying to do which i think is correct.

Ideally i think the problem is that i need to be grabbing the actual return array ("items") from the requiredValues() function and then obviously using this in my containsBlank() function as opposed to literally calling requiredValues(), not sure excatly how to do this though.

=============================== My Code:

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

function requiredValues(){ var $required = $(".required"); var items = new Array();

$required.each(function(){ items.push($(this).val()); });

return items; }

function containsBlanks(){

requiredValues().each(function(){ if($(this).val() == "") return true; else return false; }); }

===============================

Again all help is greatly appreciated.

Thanks,

Si

samiff
samiff
31,206 Points

Think about how your if/else statement would work though. If the first element passed in wasn't a blank, the if would be skipped and the else would execute a return statement. That wouldn't be good if there were more elements to check!

Also, I'm not sure if you can use ".each()" in that manner, but I'm not a jQuery expert by any means. Instead, why don't you use a technique you've already used in your isValidEmail() function. JavaScript's "indexOf" exists for arrays as well:

function containsBlanks() {
     var myArray = requiredValues();
     if (myArray.indexOf("") > -1)
          return true;
     else
          return false;
}
Simon Lovelock
Simon Lovelock
5,537 Points

Hi Sam,

Thanks very much for this. That all makes complete sense. It is trying to remember the functions that are possible to use. Got it working with that now though.

Cheers,

Si

HI stuck on the last challenge. Create a method called 'containsBlanks' that returns true if any inputs with class 'required' has an empty string in, false if not.

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

function requiredValues(){ var $inputs = $(".required"); var items = new Array();

$inputs.each(function(){ items.push($(this).val()); });

return items; } var $required = $(".required"); function containsBlanks(){ var inputs = new Array(); $required.each(function(){

            inputs.push($(this).val() == "");
        });
        return inputs.sort().pop();

This is my code the first 2 are correct, but the third "contains blanks is not.

This email is from the tutor.

Hi Neil,

You won't be able to call .each() on the requiredValues() method since this is just a plain old JavaScript array being returned. A "Bummer null" appears when there's a syntax error so that's likely the cause of that. I've asked the developer team to take a look to give better feedback.

You can use each on the $(".required") inputs in the containsBlanks function. That should help you pass.

Regards Andrew

I still am struggling with it can anyone help!

Neil