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 jQuery Basics (2014) Creating a Password Confirmation Form Improving the form validation

Michael Russell
Michael Russell
7,332 Points

Stuck on Code Challenge, Please help

Hi, I can't seem to figure this out but what I have seems like it should be the answer. The challenge is as follows: On line 11 in app.js, implement the is UsernamePresent function. Return true if the username length is greater than 0.

This is my failed solution: function isUsernamePresent() { return $username.val().length() > 0; }
This was the only section of the code I was required to change for this challenge as far as I know.

Michael Russell
Michael Russell
7,332 Points

Nevermind, I figured it out and the following code did the trick: function isUsernamePresent() { return $username.val().length() >=1; }

It was a tricky question as it was asking for the username length to be "greater than 0".

Andrew McCormick
Andrew McCormick
17,730 Points

I'm surprised that worked. I used your code just called length as a property and it worked fine:
function isUsernamePresent() { return $username.val().length > 0; }

That doesn't pass for me with the parenthesis. Without them it does. >= 1 would be the same as > 0 in this case.

Michael Russell
Michael Russell
7,332 Points

I also tried using function isUsernamePresent() { return $username.length() > 0; } with just the length as a property and it didn't work for me. I was getting frustrated and then I thought, hey '1' is more than '0', maybe it will work if I just set it greater or equal to '1', and that did the trick. Thank you for your time and help though, Andrew.

Michael Russell
Michael Russell
7,332 Points

Jason and Andrew, I understand now with my tired mind what you both are saying about length now and how it does not require the (). It would have probably worked the way I originally had it if I just removed the parenthesis. Thanks again.

Michael,

The code that you are showing here has () after length That's not using length as a property.

Michael Russell
Michael Russell
7,332 Points

I know. I don't know how that one passed. It works just fine like this, which is probably the best solution like what you guys were saying:

return $username.val().length > 0;

Hi Michael,

Yes, you're correct. Your original code would have worked without the parenthesis.

This works:

return $username.val().length > 0;

This would also work too but probably less intuitive:

return $username.val().length >= 1;
Michael Russell
Michael Russell
7,332 Points

It passed with the "length( )" the first time. I copied and pasted the passing code in my answer. However, now when I try that 'answer', it doesn't pass and it shouldn't. I must have gotten lucky that one time.

10 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

length is a property which does not accept parameters. You are calling it like it's a method.

Dry correct Answer

function isUsernamePresent() {
return $username.val().length > 0; }

Rich Braymiller
Rich Braymiller
7,119 Points

This jqery course is almost unwatchable....the guy isn't clear at all, sorry but Treehouse needs to redo the jQuery course, off to code academy for jquery...

On Line 11. >= 1;

return $username.val().length >= 1;

Hi Michael,

length is a property and not a method so you wouldn't use parenthesis with it.

Michael Russell I appreciate you giving me best answer but to be fair, Andrew McCormick answered faster than I did and we both had essentially the same answer.

Michael Russell
Michael Russell
7,332 Points

You both had the best answer. I wasn't quite understanding what Andrew was talking about until you clarified it further. It didn't sink in until you chimed in. I thank you both very much for helping me to understand this concept a whole lot more.

Michael Russell
Michael Russell
7,332 Points

Nevermind, I figured it out and the following code did the trick: function isUsernamePresent() { return $username.val().length() >=1; }

It was a tricky question as it was asking for the username length to be "greater than 0".

Matthew Goodman
Matthew Goodman
12,786 Points

Ok I am stuck, function isUsernamePresent() { return $username.val().length()>0; }

Hi Matthew,

You have to remove the parentheses after length

length is a property and not a method.

Matthew Goodman
Matthew Goodman
12,786 Points

ok so it would just be

.length >0; as this don't work I think I am losing it.

Matthew Goodman
Matthew Goodman
12,786 Points

My bad, I had another } under the code thanks for the help Jason Anello it worked well.

Something is going on with this challenge. First there is more than one way to write this code. But okay, lets play along and do it the way the instructor did, fair enough. I finally had to / figured out to copy the code from the hint function isPassorword or what ever, to PI$@@ at the moment to care, change it to $username and that worked. However the exact same code when typed in failed. But hey, I only tried like maybe thirty or forty times, with tabbing, spacing, with the keyboard upside down, etc so maybe I'm stupid and its me, right? How about put a #@$% skip button on this stuff so we can get on with it. 45 minutes of can't get it to work. And that is just for this one challange in this course. 1.5 hours lost total to stuff like this.

Hi Sean,

I'm not sure what's going on but I've tried again just now and I was able to type it in and pass task 1.

return $username.val().length > 0;