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

Daniel Hunter
Daniel Hunter
7,349 Points

Variables Challenge Task 1

I'm kind of at a loss on this one.

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

function elevatorCloseButton(pushed) {

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

    }

2 Answers

geoffrey
geoffrey
28,736 Points

Ok, the difference is in your case, your variable status is only accessible within the function. Mine was a global variable and can be accessed everywhere. In fact I didn't read correctly this part of your sentence "within the scope of the function."

My fault ;)

geoffrey
geoffrey
28,736 Points

Try something like that

var status= "";

function elevatorCloseButton(pushed) {

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

    }

Declare the variable outside the function and then if (pushed) , if true, update the content of this variable.

Daniel Hunter
Daniel Hunter
7,349 Points

Ok gotcha, thanks!

This ended up working:

function elevatorCloseButton(pushed) {

       var status= "";

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

}