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

I am having trouble with the JavaScript foundations variables code challenge

The question asks me to "Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function." After moving the var status to under the function and assigning status to "I'll close when I'm ready." It still asks me to "assign the status variable in the if statement" what am I doing wrong?

Ken Alger
Ken Alger
Treehouse Teacher

Dylan;

Can you post the code you are using? It sounds like you are very close, but without seeing the exact code it is difficult to diagnose the issue.

Thanks,

Ken

If you post your code we can help you figure out what is amiss :D

This is the original code of the challenge, question "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>

Ken Alger
Ken Alger
Treehouse Teacher

Dylan;

Yes, but what have you tried? I can post the code to solve the task, but would love to see what you have tried.

Ken

This is my code.

 <!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) {
       var status = "I'll close when I'm ready.";
        if (pushed) {
            status = "I'll close when I'm ready.";
        }

    }

    elevatorCloseButton(true);

    </script>
  </body>
</html>

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Dylan;

Perfect, thanks for posting your code.

The challenge tasks asks us to:

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

You don't want to define the variable initially, just declare it, so the code snippet would look like:

// Hoisting Code Challenge


<script>

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

    }

    elevatorCloseButton(true);

</script>

Happy coding, Ken

That makes a lot more sense, thank you for responding so quickly!

Ken Alger
Ken Alger
Treehouse Teacher

Pleased it worked out and that it helped.

Happy coding and welcome to Treehouse!

Ken

Kez Khou
Kez Khou
3,903 Points

Hmmm... interesting, thank you much Ken Alger ! I was having issues with that particular hoisting challenge as well. I didn't have the same mark up issues as Dylan Moberg , but your clarification definitely made sense and worked for me as well.