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 JavaScript Foundations Variables Hoisting

Miguel Cardenas
Miguel Cardenas
12,865 Points

Hoisting

I have tried the challenge but I still don't get it how to solve it. Any ideas?

3 Answers

Jacob Miller
Jacob Miller
12,466 Points

To follow best practices, you shouldn't declare variables inside an if block. So you need to declare the variable status inside the function, but before the if block, then simply modify the variable inside the if block.

function elevatorCloseButton(pushed) {
    // Declare the variable here, instead of inside the if block
    var status;
    if (pushed) {
        // Modify the variable here, instead of declaring it
        status = "I'll close when I'm ready.";
    }
}

i was stuck on that too...i just came off of learning python where i've not come across a situation where you declare a variable without defining it. new turf!