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

Confused about jQuery

A function can return true or false. So how can the following code:

function isPasswordValid() {
  return $password.val().length > 8;
}

Be used in the below code?

function passwordEvent(){
     //Find out if password is valid
    if(isPasswordValid()) {
    //Hide hint
      $password.next().hide();
    } else {
    //Else show hint
      $password.next().show();
    }
}

passwordEvent() has an if or else execution inside of it. But if isPasswordValid() returns true in the first function, how does passwordEvent() recognize that and execute the else function?.

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This is not the full code, so I'm just guessing here, but I think that passwordEvent runs every time you enter or remove a character in the password field. Every time it runs, it checks the if/else statement. And every time the if/else statement runs, it evaluates the isPasswordValid function and gets true or false returned.

That answered my question, thank you very much!