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 trialHeather Garcia
2,251 PointsI accomplished this challenge but I'm curious..
First off I'd like to preface that I just started this JS course yesterday and that this challenge took me over TWO hours. I really fought hard to not click the Continue button to see the teacher's code because I knew this would be a really good learning lesson for myself. When I did finally accomplish it, I anticipated that our codes would be night and day, which it was.
My only question is that I noticed he called out the ${rank} variable in the beginning and left the value empty. ie: let rank = '';
I didn't include my rank variable until the end when I was calculating the score using
if ( score === 5 ) {
rank = (Gold
);
}
Page functioned correctly, however I am wondering if there are any caveats to not include the initial variable call.
Thanks for your help and happy coding!
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsIt's common to declare an empty variable and then change its value later on in the script.
You can do that because when you declare it you have the let
keyword which allows you to do this, unlike const
Somewhere else in the program you have a variable called score with a value of 5. And that's what allows you to assign a new value to rank
because that condition has been met and the action for that particular code block is to assign a new value to the rank
variable.
If however you were to use let rank
again as the variable that would completely redeclare it which would cause problems in your script.
Heather Garcia
2,251 PointsHeather Garcia
2,251 PointsThat makes so much sense, thanks so much for clarifying! I'll keep that as a best practice. Cheers