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 trialtoniclawson
Courses Plus Student 459 PointsProblem with the exercise after watching the Hoisting video.
I've watched the video on hoisting three 3 times, but in the examples given, it doesn't explain fully in what to do in the exercise quiz. The video gave examples of how to add to more lines to the variable. Example Var Color = "blue", name "Jim", number 10;
I don't understand how to correct the exercise given after watching the video without the presenter fully explaining what to do in this type of scenario. Do I type console.log ( type in phrase here) after evelatorClosebutton statement?
toniclawson
Courses Plus Student 459 Points-
Here's my original answer do to following video cut, but it's wrong :
function elevatorCloseButton(pushed) { var status = "I'll close when I'm ready.";
if (pushed) { console.log ("I'll close when I'm ready.") } console.log ("I'll close when I'm ready." ) } elevatorCloseButton(false); elevatorCloseButton(true);
Phillip Gibson
13,805 Pointstoni clawson , I edited my answer below with more detail. I hope this gets you on the right track. :)
1 Answer
Phillip Gibson
13,805 PointsPay special attention to the video starting around the 4:40 mark and up through 5:13. This is where he explains what "hoisting" actually is, and how you can change that code to follow best practice.
EDIT:
In summary, the idea of "hoisting" is that any variable declared with a var
within a function, JavaScript will, behind the scenes, move the declaration of that var
to the top of the function. The initialization will stay wherever you put it. So to follow best practice, you just go ahead and do this yourself.
(Declaration is when you "declare" a variable exists, ie. var thisIsAVariable;
, while initialization is when that variable is given a value, ie. thisIsAVariable = "I'm a string";
. They can be done at the same time, ie. var thisIsAVariable = "I'm a string";
, but it's not necessary, and as the video points out, JavaScript will probably move your declaration anyway.)
So for the challenge, that means moving the declaration of status
to the top of the function. But you don't want to move the initialization because that would then change the functionality. The console.log()
calls were just used for demonstration purposes, and aren't related, so you don't have to do anything with those.
In short, the final answer is this:
function elevatorCloseButton(pushed) {
var status;
if (pushed) {
status = "I'll close when I'm ready.";
}
}
toniclawson
Courses Plus Student 459 PointsPhillip, thank you for your explanation; it's superb and I completely understand what I did incorrectly. Great to know it wasn't necessary to include the console log, because originally I thought I had to follow the protocol listed in the video exactly.
Phillip Gibson
13,805 PointsGlad to help. :)
toniclawson
Courses Plus Student 459 Pointstoniclawson
Courses Plus Student 459 PointsPhillip, thank you for for advising me to re-watch the video. However, the video doesn't explain how to answer the question correctly, it's vague...or maybe it's just me. I completely understand what hoisting is which means to state the "var" statement before I give an "if" statement to define content before reloading the webpage. Unfortunately, it's not clear in the video in how to answer the question for the hoisting exercise. I'm sorry but I don't get it. I was able to successfully understand the other exercises before hoisting, but kindly need your input to find out how to answer the question correctly. I sincerely appreciate any advice that you can share with me.
P.S. I did space out my statements but upon re-posting my comments, all my original, answer, and video statements appears to be on two lines when it shouldn't be.
Here's the question: Alter the 'elevatorCloseButton' function to follow the best practices in declaring variables within the scope of the function.
function elevatorCloseButton(pushed) {
Here's my original answer do to following video cut, but it's wrong :
function elevatorCloseButton(pushed) { var status = "I'll close when I'm ready.";
if (pushed) { console.log ("I'll close when I'm ready.") } console.log ("I'll close when I'm ready." ) } elevatorCloseButton(false); elevatorCloseButton(true);
Here's the Video Example: // Hoisting function doSomething (doit) { var color = "blue"; var number; if (doit) { color = "red"; number 10; console.log ("Color in if (){}", color) } console.log ("Color in after (){}", color) } doSomething(fasle); doSomething(true);