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

Ai Francisco
Ai Francisco
5,640 Points

Js Hoisting?

Hello guys! I can't get this challenge task, so I badly need your help. It says that I should assign the 'status' in the if statement, which I did like below. I moved var status = "I'll close when I'm ready."; inside (just below) the function. What other things is need to do to make this code work? Thank you in advance for your help!

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

2 Answers

In this case we're looking to have the status variable only get set if pushed evaluates to true (so inside of the conditional) but we'd like to use it outside of the conditional. To this end we'll need to declare the status variable outside of the conditional and set it to "I'll close when I'm ready." inside the conditional. Make sense?

Ai Francisco
Ai Francisco
5,640 Points

Thank you Geoff and Jeff, I did not come up with that, it makes a lot more sense now!! :))