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

Is this okay solution or should I write it simpler?

On line 11 in app.js, implement the isUsernamePresent function. Return true if the username length is greater than 0.

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

Hello Marko,

You haven't indicated the video or file to which you are referring; thus, it is hard to say if the solution you provided is simplest without a sense of context; however, note the name of your function: isUserPresent(). One would expect this to check if something - namely the user- exists or is active or something like that. Instead, the function simply gets the length...you may do well to provide your function with a more appropriate name?

it's jQuery basics last code challenge of password section.

4 Answers

Hi Marko,

Your code does satisfy the request of that challenge so nice job. In terms of improving your code quality, here are two things to think about:

  1.  Avoid writing the whole function inline. Even if the function only has one line of behavior, keeping function contents separate from the declaration line is a good habit to get into.

  2. I'm not sure if it's mentioned in this course, but when you're working with string length as a validation rule, it's important to "trim" the extra stuff off of the value before you check its length. It's as simple as adding a function call in between val() and .length. Example:

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

Without that trim() call in there, someone can enter a few empty spaces into the field and it will pass this validation! :)

he doesn't mention the trim() function... interesting, thank you for this. Of course I didn't wrote it inline it just that forum formated it this way

Unless you're making a number of functions similarly named, I would tend to agree with Joe Bruno, maybe change the name of the function to something like isUsernameFilled

You could also make it shorter and/or more compact, but it may not be as clear:

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

Alternatively, you could space out the function a bit to make it easier to read, so it really depends on what you're going for when you say 'simple'. The function itself has a simple... function. :)

isUsernameFilled is default value not my name.

Hi Marko Mlinarevic,

From first glance, it appears that you've written some pretty clean code. There's always ways to simplify code, but the code you've written seems to suffice just fine.

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