Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Variables Hoisting

Java Script Challenge :function elevatorCloseButton

why is it telling me to call the status in the if statement?

\\html

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Variables</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Variables: Hoisting</h2>

<script>

function elevatorCloseButton(pushed) {
  var status = "I'll close when I'm ready.";
    if (pushed) {
        console.log(status);
    }

}

elevatorCloseButton(true);

</script>

</body> </html>

i'm at the same spot and also would like to know the answer....

1 Answer

Hi Susan,

The code they are looking for is this:

    function elevatorCloseButton(pushed) {
      var status;
        if (pushed) {
          status = "I'll close when I'm ready.";
        }
    }

Even though the code would function the same with the var keyword inside the if statement (the way it is when you start the challenge), it's a best practice to define all the variables that you're going to use inside the function at the top - even before you've used them.

It makes it easier to the read code if you can see all the variables that are going to be in play in the function right at the top.

Thank you