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

Having trouble with "Alter the 'elevatorCloseButton' funciton to follow the best practices in declaring variables within the scope of the function."

JavaScript is really hard for me so far...got somewhat far on the first Badge for JavaScript Foundations...not sure how to solve this one

<script>

function elevatorCloseButton(pushed) {


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

}

elevatorCloseButton(true);

</script>

8 Answers

Aaron Chiandet
Aaron Chiandet
9,826 Points

I've watched the video numerous times now and I'm still miffed. Obviously I'm assigning the 'status' in the if statement incorrectly. My current answer looks like this...

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

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

}

elevatorCloseButton(true);
Aaron Chiandet
Aaron Chiandet
9,826 Points

I just figured it out, sometimes you feel so stupid when you overlook the most minuscule thing. 99% of the time it's right in front of your face lol...

function elevatorCloseButton(pushed) { var status;

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

}

elevatorCloseButton(true);
Aaron Chiandet
Aaron Chiandet
9,826 Points

I can't seem to edit my last post, the correct code is as follows.

function elevatorCloseButton(pushed) {
var status;

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

}

elevatorCloseButton(true);

Chris, you need to declare the variable 1st under the function, then set it under the if statement.

Check out the video at approximately the 4:20 point when he gives the number example.

Hope this helps.

Thanks Joe...I actually figured it out like 25 minutes after posting this...lol...it's so frustrating when I'm completely stumped but the solution was so easy once I did it

Had problems with this, and the 4:20 (potheads??) in the video nailed it for me...love when I get the "AHA!" moment!

Thanks for that Aaron. I too have been stuck on this one for ages :)

Mike F.
Mike F.
11,994 Points

Thanks Aaron. I was banging my head against the wall then realized, after looking at your code, that I was still using "var" in the if block.

Ben Stevenson
Ben Stevenson
9,360 Points

Ah ha. Thank you Aaron.

David Taber
PLUS
David Taber
Courses Plus Student 3,574 Points

Wow if you are used to programming in strongly typed languages this is rough.

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

This was apparently wrong. I do not like to have uninitialized local variables.

Alina Kovalenko
Alina Kovalenko
5,553 Points

When it seems hard at first and you keep digging will always get that sweet "Aha!" moment ))... The variable here must be declared first under the function. You can give any name to it ( unless it's a reserved name by JavaScript ). This code worked for me) <script>

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

}

elevatorCloseButton(true);

</script>
Ashley Wilson
Ashley Wilson
3,980 Points

Thanks Aaron, this one threw me too... Javascript makes my head hurt.