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 trialChris Sicam
4,482 PointsHaving 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
9,826 PointsI'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);
Joe Thompson
4,777 PointsChris, 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.
Chris Sicam
4,482 PointsThanks 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
Lee Anderson
7,463 PointsHad problems with this, and the 4:20 (potheads??) in the video nailed it for me...love when I get the "AHA!" moment!
Stu Cowley
26,287 PointsThanks for that Aaron. I too have been stuck on this one for ages :)
Mike F.
11,994 PointsThanks 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
9,360 PointsAh ha. Thank you Aaron.
David Taber
Courses Plus Student 3,574 PointsWow 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
5,553 PointsWhen 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
3,980 PointsThanks Aaron, this one threw me too... Javascript makes my head hurt.
Aaron Chiandet
9,826 PointsAaron Chiandet
9,826 PointsI 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;
Aaron Chiandet
9,826 PointsAaron Chiandet
9,826 PointsI can't seem to edit my last post, the correct code is as follows.