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 trialWenwei Lu
10,897 PointsAlter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the fun
function elevatorCloseButton(pushed) {
var status = "I'll close when I'm ready.";
if (pushed) {
status = "I'll open when I'm ready.";
}
console.log(status);
}
elevatorCloseButton(true);
Why it is wrong?
2 Answers
Adam Sackfield
Courses Plus Student 19,663 PointsSo what you need to do is declare the variable first then inside the if statement change the variable. So something like this would work.
function elevatorCloseButton(pushed) {
var status;
if (pushed) {
status = "I'll close when I'm ready.";
}
}
elevatorCloseButton(true);
See how the var is declared first and then changed by dropping the keyword var in the if statement. This would make it easier if you had an else statement to change the same variable again with the same method.
Wenwei Lu
10,897 PointsThank you Adam!
Adam Sackfield
Courses Plus Student 19,663 PointsYour welcome! Wenwei Lu I also edited your post to add correct markdown, if you click edit now you will be able to see how to embed the code as above :)
Danny Olsen
10,221 PointsDanny Olsen
10,221 PointsThis helped me a Ton! thx man <3
Adam Sackfield
Courses Plus Student 19,663 PointsAdam Sackfield
Courses Plus Student 19,663 PointsAnytime Danny Hansen
Kevin Matuszek
Courses Plus Student 6,252 PointsKevin Matuszek
Courses Plus Student 6,252 PointsThanks, Adam. I was stuck on this and couldn't figure out the solution based off the prompts being given in the challenge.