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 trialMarie 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,679 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,679 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!