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

Stuck on this question! Javascript..

Hi guys I'm stuck on this question! any help would be appreciated!

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

function elevatorCloseButton(pushed) {

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

    }

    elevatorCloseButton(true);

4 Answers

When declaring a variable in JavaScript, inside any scope, the best practice is to declare the variable at the beginning of the scope block.

Any value assignment to that variable can happen later on, when it's necessary.

Currently, var status = "I'll close when I'm ready."; is doing both the declaration and the assignment on the same line in the same place which isn't the beginning of the scope block (in this case the beginning of the function). But the JavaScript compiler (interpreter) sees that as two statements — the declaration and the value assignment.

Split the declaration and the value assignment into two statements, and move the declaration to the top of the function, leaving the assignment where it is.

You do that because the compiler is going to do this anyway, and you want to avoid confusion. This is something that's called variable hoisting.

function someFunc () {
  // something happens here
  var someVar = "some value"; // The compiler sees this as: var someVar; someVar = "some value";
}

// The declaration is hoisted to the top of the appropriate scope during the compile time, but the value assignment happens during the execution of the code so the code (as far as the compiler is concerned) becomes:

function someFunc () {
  var someVar; // Declaration hoisted to the top of the function
  // something happens here
  someVar = "some value"; // value assignment stays where it was originally
}

As I mentioned, this can cause confusion, so it's best to write the code the way the compiler is going to see it.

Done! thanks!

And I was just about to post a solution. I'm glad you've figured it out on your own, that's always for the best. :)

Thanks! I have tried this and still no luck! where a m i going wrong?

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

    }

    elevatorCloseButton(true);

I've edited my response with a more detailed example. See if that helps.

Could you just write the code to the answer then explain it. would be much easier. thanks

Stuck on this question like you. Did you ever figure this one out??