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 trialKyle Pierre-Samson
10,718 PointsSyntax & different ways to do things
I have a couple of questions:
first, whilst writing the multiple prompts to ask the questions I forgot to declare the variables as multiple "const". I actually did not declare them as anything and the code ran just fine.. I am assuming this is because it is automatically translated into a "var". Is this correct?
Secondly, What was the point of selecting the <main> and putting it into a constant at the beginning? Again I did mine differently and it worked fine, I just don't understand why Guil chose this different method which seemed odd to me. Is it better practice?
My code for reference:
document.querySelector('main').innerHTML = <h2>You got ${correct_answers} questions correct out of 5.</h2>
<p>Crown earned: ${player_rank}
1 Answer
Jamie Moore
3,997 PointsHey Kyle,
You are right in a way. But there are a few differences, this answer from stackoverflow phrases it way better than I ever could:
If you use var the variable is declared within the scope you are in (e.g. of the function). If you don't use var, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches.
As for selecting the main
element, this works either way. The benefit of storing it in a variable is that it's easier to read (subjective) and it's reusable. If the codebase grows, you can just keep using the declared variable for main
rather than having to select it again every time.
Hope this helps!
Kyle Pierre-Samson
10,718 PointsKyle Pierre-Samson
10,718 PointsThanks for your help!