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 Build an Interactive Website Form Validation and Manipulation Checking Values

sort and poping the values

I get why he is push the values to blanks variable but I don't get why he is sorting them out and poping the last value.

3 Answers

Hey don,

The reason he is popping off the last value is because the sort method will rearrange the array of true or false (boolean) values so that all of the false values will be in the front of the array and all of the true values will be at the back of the array.

Since the .each loop is returning true every time there is a blank, if there is a blank, the last item in the array will be true. Which makes the function containsBlanks return true, which we can then use in other parts of the code in conditionals to disable the submit button if containsBlanks is true (because we dont want to submit if there are blanks).

Hope this helps a little =}

Great explanation!

it pops out just one value right?

if there is no empty field then the array will be filled with false values and the so the containsBlanks will return false? -_- rawr!

Precisely! it pops out the very last value. So if there are ANY true values, a true value will be popped out. If there are no true values, then a false will be popped out, and the entire function will return false.

If i remember correctly he later used the function containsBlanks in a conditional but he put a "!" before the name. a "!" will negate the function, so that if the function returns true (theres blanks) then later on in the conditional the "!containsBlanks()" will make it false. But if containsBlanks returns false (no blanks), then "!containsBlanks()" will return true, and thus activate the submit button.

thanks Austen Payan!