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.

Marie Veverka
12,117 PointsHoisting
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

Geoff Parsons
11,667 PointsHey 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
12,117 PointsHi 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?

Geoff Parsons
11,667 PointsVery 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
12,117 PointsYay!!! That's the ticket! Thanks Geoff!