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 trialmickey Odunikan
4,983 PointsVariable best practices?
Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function.
<!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) {
var status = "I'll close when I'm ready."
if (pushed) {
status;
}
}
elevatorCloseButton(true);
</script>
</body>
</html>
Zachary Green
16,359 Points^^Hit it on the nail
1 Answer
MUZ140146 Warrant Ganye
7,120 PointsThanks a tonne :)
Dan Garrison
22,457 PointsDan Garrison
22,457 PointsThe problem is that you are declaring the value for the variable before the if statement. The value should only evaluate to "I'll close when I'm ready." if the value passed to it is true. So your function should read something like:
So basically you first declare the variable at the beginning of the function using the var keyword. Then you call that variable again within the if statement. You don't use the var keyword here because that would create a new variable called status instead of calling the original variable.