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

JavaScript JavaScript Foundations Variables Hoisting

Problem 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?

Phillip, 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.

  1. What do you do when you've moved the "Var" statement above the "If" statement as described in the video, and you're told that you got it wrong?
  2. What do you next after you followed exactly the video sections 4:40 to 5:13?
  3. What other var or other statements are required to answer the question correctly??? ***********************************************************************************************************************************************************************************
  4. Original Context for Hoisting exercise

function elevatorCloseButton(pushed) {

    if (pushed) {
      var status = "I'll close when I'm ready.";    
}

}
elevatorCloseButton(true);
  1. 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);

  2. 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);

  1. 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);

toni clawson , I edited my answer below with more detail. I hope this gets you on the right track. :)

1 Answer

Pay 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.";
    }
}

Phillip, 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.

Glad to help. :)