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

General Discussion

Hoisting Challenge

Does anyone have a clue what there looking for in this challenge:

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

Code:

<script>

    function elevatorCloseButton(pushed) {

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

    }

    elevatorCloseButton(pushed);

    </script>

Please Help

2 Answers

You need to create the variable in the beginning of the function. This is best practice because any variables declared in the function will be hoisted to the top of the function. Even if the variable is declared in a conditional statement like "if", as long as it's within the function, the variable will be hoisted to the top. So the code above is actually interpreted like this:

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

}

elevatorCloseButton(pushed);

</script>

I got it! They want me to declare the variable above the if statement and give it a value inside the if statement.