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

Code challenge: Checking values

The task for the third part of the Code challenge: Checking Values does not make sense.

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

The confusing part is what this function is evaluating. The "array of required values" could be:

  • passed to the function as an argument

  • obtained by calling the requiredValues() function written in the previous task

  • obtained de novo by selecting $(".required") as we've done already

After many failed attempts, I figured out that the third interpretation is the only one which validates.

I think this could be clarified by asking whether "input boxes of the class "required" contain blanks", not an array of required values, which presumably must be generated by code somewhere else.

cheers,

Casey

11 Answers

I actually had the same issue with this question --

It is very easy to take "if an array of required values has any blanks in it" to mean:

a) That the method we're being asked to construct should take an argument of an array. or b) That the method should first create an array out of the required values, and then determine if any values in that array are blank using the techniques taught in the video.

The problem there is that, as mentioned above, that any JS array created won't have an each() and a $(this) in the sense we've just worked with, creating a rather large number of possible incorrect outcomes.

The ironic thing is that the correct answers are much nearer to what is done in the video, the wording of the code challenge just seems to imply that we should recombine the skills we've learned in a new way -- one which unfortunately is impossible.

I definitely reconfigured my answer dozens of times before coming here, not having realized that the base premise of my response (an each()/this for the items in an array) would doom my attempts to failure.

Here is what worked:

function containsBlanks(){
    var blanks = new Array();
    $(".required").each(function(){
         blanks.push($(this).val() == "");
    });
    return blanks.sort().pop();
}

The following things do NOT validate:

 function containsBlanks(){
    var blanks = new Array();
    var req = requiredValues(); //written in the previous section
    req.each(function(){
         blanks.push($(this) == "");
    });
    return blanks.sort().pop();
}

Nor does:

function containsBlanks(req){
    var blanks = new Array();
    req.each(function(){
         blanks.push($(this) == "");
    });
    return blanks.sort().pop();
}

Nor does:

function containsBlanks(req){
   var blanks = FALSE;
   var len = req.length;
   for (x = 0; x < len; x++){
      if (req[x] == "") blanks = TRUE;
   }
   return blanks;
}

Do these others contain errors?

I just don't think it's clear what the "array of required values" should point to.

Casey

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Casey,

Sorry you're having trouble. You can pass this test in many ways as you described, and for that reason there is that ambiguity.

The only thing that's mandatory is the containsBlanks function that returns true or false. There are no parameters.

If you paste in your latest code or use a codepen to show where you are we can use that as a starting point and get you to where you need to be to pass.

Regards Andrew

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
function containsBlanks(){
    var blanks = new Array();
    var req = requiredValues(); //written in the previous section
     req.each(function(){
             blanks.push($(this) == "");
     });
    return blanks.sort().pop();
}

The above didn't work because the req is a JavaScript array and doesn't have the each method that returns this in the way you're using it. You're using it like a jQuery selection. The other two with req in the method signature isn't required by the task, so it would fail.

So a combination of:

function containsBlanks(){
    var blanks = new Array();
    var req = requiredValues(); //written in the previous section
     req.each(function(){
             blanks.push($(this) == "");
     });
    return blanks.sort().pop();
}

And:

function containsBlanks(req){
   var blanks = FALSE;
   var len = req.length;
   for (x = 0; x < len; x++){
      if (req[x] == "") blanks = TRUE;
   }
   return blanks;
}

Would work like this:

function containsBlanks(){
   var blanks = false;
   var req = requiredValues(); //written in the previous section
   var len = req.length;
   for (x = 0; x < len; x++){
      if (req[x] == "") blanks = true;
   }
   return blanks;
}

You would also need to lower case the FALSE and TRUE too or else ReferenceErrors may occur.

So your correct answer and the one above are at least two ways of passing this challenge.

Thank you Jesse for saying far more eloquently what I was trying to convey. It sounds like it should be a general function to evaluate any array of strings.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Thanks for your feedback. I've changed the wording now and hopefully this won't trip as many people up.

It now reads:

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

Hello everyone. I need some help. I have tried reading all the forum topics on this, but none seemed to work for me. Please show me how to do this correctly

QUESTION: "Create a method called 'containsBlanks' that returns true if any inputs with class 'required' has an empty string in, false if not."

MY RESPONSE: Exactly as I typed it.

var $submit = $(".submit input");
var $required = $(".required");
function containsBlanks(){
   var blanks = false;
   var req = requiredValues(); 
   var len = req.length;
   for (x = 0; x < len; x++){
      if (req[x] == "") blanks = true;
   }
   return blanks;
}
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Do you still have the requiredValues method in your code? What's the hint that shows saying it's wrong?

I was also having significant problems with this section. So much so that I simply pasted the code in from the download file. I still have no idea what the map( ) method does and why that seemed to work when all else failed.

I also seems that there are a number of instances in which I could update one function definition, but that would result in none of the others working. Even though the old function definitions hadn't been touched at all.

Please help, Andrew.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The map() method goes through each element you've selected and runs a piece of code that you specify within the brackets. The resulting value or the value that is returned is added to an array.

Imagine I have several inputs with the values "Hello", "Good bye", and "Hello" again.

  var myArray = $("input").map( function() {
      return $(this).val();
  });

The above code would result in myArray being set to an array like this ["Hello", "Good bye", "Hello"]. The val() method returns the string value from the text input.

  var myArray = $("input").map( function() {
      return $(this).val() == "Hello";
  });

The above code here will result in myArray will be set to the array [true, false, true] as this $(this).val() == "Hello" will return true when the value of the input is "Hello" and false if not.

When code challenge steps fail when they once worked before is because there is a syntax error with the JavaScript you've written. Put a ) or a } in the wrong place, put a return somewhere where you shouldn't, something like that. If you paste your code after another attempt we'll try and figure out where you're going wrong.

This doesn't seem to work for some reason....

function containsBlanks() {
    $('.required').each(function() {
        if ($(this).val() == "") {
            return true;
        }
    });
    return false;
}