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

I need help in this challenge

this challenge say this.

Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function.

my answer is this

function elevatorCloseButton(pushed) {

  var status = "open",
        elevator;
        name = "elevator" 

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

}
elevatorCloseButton(false);
elevatorCloseButton(true);

but the system say this.

You should only need to do 'var status' once. I do not understand what is wrong and leave only "status", but still wrong can you help me please?

index.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 = "open",
            elevator;
            name = "elevator" 

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

    }
    elevatorCloseButton(true);
    elevatorCloseButton(true);


    </script>
  </body>
</html>

1 Answer

Justin Estrada
Justin Estrada
34,995 Points

Declare the variable status at the beginning of the function then remove the declaration of the variable status within the If conditional block.

like this:

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