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

Javascript: Checking Values quiz question 3/3

Here is the challenge: "Create a method called 'containsBlanks' that returns true if the array has any blanks in it, false if not."

Here is my code: function isValidEmail(email) { return email.indexOf("@") != -1; }

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

function containsBlanks() { return values.sort().pop(); }

Apparently that is not working. Could anyone point me in the right direction?

3 Answers

Deleted User

The problem is that in your function containsBlanks() you're looking for a variable that is not global (the array values), this means that the variable is not available for every function in your code but only for the one that contains it, in your case requiredValues().

What you could do is to make this variable global and take it out of any function (in your case place the "var values = new Array();" line before you start to declare the requiredValues method this will allow you to call the array from any function.

Also, what you're returning in the containsBlanks method doesn't work, because you haven't created anything that defines if the input is blank or not. So what you can do is to use the same code as in the requiredValues method :

$(".required").each(function(){
    values.push($(this).val() //And here tell it to be equal to "" 
    leaving the space between the "" blank so it knows it has 
    no value in it);
});

I hopes this helps, let me know if you could do it.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

+1 Diego

But be careful when doing var outside of the scope of a function; for example:

var color = "red";

function setColor(){
    var color = "blue";
}

setColor();
alert(color); //Would alert "red"

And...

var color = "red";

function setColor(){
    color = "blue";
}

setColor();
alert(color); //Would alert "blue"

Jim talks about scope in JavaScript Foundations

Deleted User

Thank you!