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

Nicole Raglin
Nicole Raglin
2,522 Points

Var-Hoist Quiz

What am I doing wrong???

<!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 = "I'll close when I'm ready.";
  console.log("status in if (){}", status);
    }

}

elevatorCloseButton(true);

</script>

</body> </html>

Mike Bronner
Mike Bronner
16,395 Points

Please explain in more detail as to what this code is or isn't doing that is contra to your expectations. Also, if it is part of a challenge, please link to the challenge.

Nicole Raglin
Nicole Raglin
2,522 Points

Hi Mike. It is a challenge and part of a challenge!

http://teamtreehouse.com/library/hoisting

I cannot seem to execute it properly...but I did just notice that I skipped a whole section of Java!

:)

Mike Bronner
Mike Bronner
16,395 Points

Just a quick comment, and it's not meant to be persnickety. Don't confuse Java with JavaScript. They actually have nothing at all to do with each other. :) I see those two terms confused a lot, and using them incorrectly may cause more confusion.

Nicole Raglin
Nicole Raglin
2,522 Points

I don't think you are being persnickety. Your correction is correct and taken to heart. I am still getting the error

You should assign the 'status' in the if statement.

I am a little clueless as I missed the whole basics section. I am doing it over.

1 Answer

Mike Bronner
Mike Bronner
16,395 Points

Ok, this wasn't too hard, although it might be a bit confusing as to what they were asking.

They ask you to declare the variable in the function following best practice, which says to declare variables toward the top of the function, rather than interspersed.

Really the only thing you need to do is declare the status variable right after the function opens, and remove the declaration (not the assignment) from within the if statement.

Let me know if that gets you on track, or if it is still unclear. If so, I can help you further with some pseudo-code for the function. :)

~Mike

Nicole Raglin
Nicole Raglin
2,522 Points

I don't think you are being persnickety. Your correction is correct and taken to heart. I am still getting the error

You should assign the 'status' in the if statement.

I am a little clueless as I missed the whole basics section. I am doing it over.

Mike Bronner
Mike Bronner
16,395 Points

In your code above you are declaring and assigning the variable status a value. Don't assign it anything when you declare it (that's not necessarily best practice, just this scenario). After that it should work.

Nicole Raglin
Nicole Raglin
2,522 Points

Finally! Thank you! You were a huge help! Now I am going to go over JavaScript again...from the beginning this time! Thank you!!!

Mike Bronner
Mike Bronner
16,395 Points

That's great to hear! :)