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

email validation code challenge 3 for containBlanks

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

I've managed to get past the challenge with the help of the forum by creating a new array in the containBlanks method as described in many other posts on this topic.

I feel that there should be a better way to do it because the previous code challenge already returns an array with the values of the inputs with class required.

Here's my logic... Call that previous function and just check the array for the indexOf(""). If that value does not equal -1 then there must be blank values in the array and the containBlanks function should evaluate to true.

Here's my code... var $required = $("input.required");

function requiredValues() {
var inputValues = new Array();
    $required.each(function() {
    inputValues.push($(this).val());
    });
    return inputValues;
}

function containBlanks() {
    requiredValues().indexOf("") != -1;
}

I'm trying to call the requiredValues() method and then compare the indexOf("") in the returned array against -1. If there's a blank("") then the index value of that blank will not be -1 and the containBlanks function shoudl return true. If not, it shoudl return false.

But I'm missing something and I don't know what it is. Help mods!

2 Answers

Your logic is sound, but you're missing the return statement in the containsBlanks() function:

function containsBlanks() {
  return requiredValues().indexOf('') !== -1;
}

Without an explicit return statement, a JavaScript function will return undefined.

And I recommend you always use the identity operator (=== / !==) and not the equality operator (== / !=).

Ahh! Thanks Dino. That worked. I should have got that from one of the previous methods used in the challenge, isValidEmail(). Also, the error I was getting, suggesting that I had to define the method containsBlanks(), should have been a hint that my function was returning undefined. I just didn't make the connection.