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
Tommy Gebru
30,164 PointsWhat's wrong with my code?
Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function. Bummer! You need to declare 'status' at the top of the 'elevatorCloseButton'.
<!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>
MOD edit: This question is in relation to this challenge: https://teamtreehouse.com/library/hoisting
3 Answers
Andrew McCormick
17,730 PointsTeame, You didn't change anything here from the code that the challenge initially gives you.
The video before that challenges explains hoisting in javascript which is what they are after here. According to the video, since the variable declarations are hoisted it's best practice to declare the variables at the top of the function and then assign them values when needed. So in the challenge you should declare your variable at the top of the function outside the if statement while still assigning it, it's value inside the if statement.
Example:
function name() {
var buckeye; //this is declaring a variable at the top of the function
if ( blah = blah) {
buckeye= "Go Bucks!"; //this is assigning a value to the variable that is declared at the top of the function.
}
}
Tommy Gebru
30,164 PointsThis I have tried plenty however the same statemen pops up! Bummer! You should assign the 'status' in the if statement.
function elevatorCloseButton(pushed) {
var status = "I'll close when I'm ready."
if (pushed) {
status = "I'll close when I'm ready.";
}
Andrew McCormick
17,730 Pointstwo things:
- you are declaring the variable and assigning it a value at the top of the function which is not what is needed here nor what they are asking for. just declare it.
- you are missing a semi colon
Tommy Gebru
30,164 PointsThanks just got it , you saved the day
Andrew McCormick
17,730 Pointsawesome!