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 trialNazaam Kutisha
7,667 PointsHelp with this challenge
Not clear on what is happening. I've defined the variable with global scope, but Im still not clearing the challenge. What am I missing???
My code response was
<script> var status function elevatorCloseButton(pushed) {
if (pushed) {
status= "I'll close when I'm ready.";
consol.log(status);
}
}
elevatorCloseButton(true);
</script>
Hayden Taylor
5,076 PointsI know where the link is haha but it didn't work for me is what I was saying but the answer below outlines everything you need to know
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Nazaam,
By "global scope" I'm assuming you declared the variable outside the function?
The instructions state that you should declare the variable within the scope of the function but follow best practices in doing so.
You start off with:
function elevatorCloseButton(pushed) {
if (pushed) {
var status = "I'll close when I'm ready.";
}
}
Javascript doesn't have block level scope, it has function scope instead. So just because the variable is declared inside the if
block that does not mean it's only available inside the if block. It is still available outside the if block but still within the function.
What javascript does is hoist that variable declaration to the top of the function.
Here the instructions are asking you to make that explicit.
function elevatorCloseButton(pushed) {
var status;
if (pushed) {
status = "I'll close when I'm ready.";
}
}
So here we're making it clear the way javascript would treat it. The variable is declared within the scope of the function and then the assignment statement can happen inside the if block.
Nazaam Kutisha
7,667 PointsThere is a link to the code challenge under related content . My code response was:
<script>
var status
function elevatorCloseButton(pushed) {
if (pushed) {
status= "I'll close when I'm ready.";
consol.log(status);
}
}
elevatorCloseButton(true);
</script>
Jason Anello
Courses Plus Student 94,610 PointsYou were close and had the right idea but just went too far up with the variable declaration.
Nazaam Kutisha
7,667 PointsWhats up Jason, Brillant reply. This syntax passed the challenge. Thanks for the prompt response bro.
Jason Anello
Courses Plus Student 94,610 PointsYou are welcome.
Hayden Taylor
5,076 PointsHayden Taylor
5,076 PointsCould you post some code, the question and the task at hand please. Although it says hoisting ... it doesn't link anywhere.