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

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!