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

Wenwei Lu
Wenwei Lu
10,897 Points

Alter 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

So 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.

Danny Olsen
Danny Olsen
10,221 Points

This helped me a Ton! thx man <3

Anytime Danny Hansen

Kevin Matuszek
Kevin Matuszek
Courses Plus Student 6,252 Points

Thanks, Adam. I was stuck on this and couldn't figure out the solution based off the prompts being given in the challenge.

Your 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 :)