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

Confused with what the challenging is asking...

I am currently stuck with the following question. I tried to put the "var status" in different places within the elevatorCloseButton function but I keep getting it wrong.

Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function.

<!DOCTYPE html> <html lang="en"> <head> <title> JavaScript Foundations: Variables</title> <style> html { background: #FAFAFA; font-family: sans-serif; } </style> </head> <body> <h1>JavaScript Foundations</h1> <h2>Variables: Hoisting</h2>

<script>

function elevatorCloseButton(pushed) {

    if (pushed) {
         var status = "I'll close when I'm ready.";
    }

}

elevatorCloseButton(true);

</script>

</body> </html>

1 Answer

Jeremy Faith
PLUS
Jeremy Faith
Courses Plus Student 56,696 Points

First declare the variable status inside the function then inside the if statement you can define it.

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

}

Thank you sir!

Tommy Gebru
Tommy Gebru
30,164 Points

Hey Yi Ren,

In the video, Hoisting with Jim Hoskins, best practices here would be to define your variables at the top of the function the way Jeremy Faith has done

  var status;

and later assign a value to the variable that is "status"

 status = "I'll close when I'm ready.";

Hope that helps ;)