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

Marie Veverka
Marie Veverka
12,117 Points

Hoisting

Hi there,

I am not sure how to alter this to fit best practice . . .

<script>

function elevatorCloseButton(pushed) {

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

}

elevatorCloseButton(true);

</script>

I've tried several things and I am not sure where I am going wrong . . .

Thanks in advance for helping!

3 Answers

Hey Marie,

The "best practices" they mention are mentioned in the previous video, specifically the scope of variables within the function. The variable "status" should be declared outside of the if statement.

Marie Veverka
Marie Veverka
12,117 Points

Hi Geoff,

Thanks! I tried that (or I thought I did) as follows:

<script>

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

}

elevatorCloseButton(true);

</script>

but it still not working. I am not sure what I am missing. Any thoughts?

Very close, you don't need to set it outside of the if statement though, only declare it.

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

}

This way it will only have the value "I'll close when I'm ready." if pushed is true but can still be used outside of the scope of the conditional.

Marie Veverka
Marie Veverka
12,117 Points

Yay!!! That's the ticket! Thanks Geoff!