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

Best practice for conditions comparing value to number range?

My code worked for this challenge and I feel that it is relatively simple and clean. However, in the section where it compares the variable containing the number of correct answers ('score' in my code) I used different checks to receive the same results. Instead of using ' >= 3' in the silver check, I used ' > 2 ' which, to my understanding will only give a true if the number is, in fact 3 or 4. I used the same logic for 1 to 2 : ' > 0 '. My question is which way is the best practice to be in? Are there possible situations in which the way I formatted it would not work?

Thanks in advance!

(code for reference)

//determine and display crown received based on number of correct answers if( score === 5 ) { document.write('You got a gold crown!'); } else if( score > 2 ){ document.write('You got a silver crown!'); } else if( score > 0 ) { document.write('You got a bronze crown!'); } else { document.write('You did not receive a crown.'); }

1 Answer

Steven Parker
Steven Parker
229,657 Points

The comparisons may differ if the value tested is not an integer (for example: 2.5).

But as long as the value is always an integer, there's no functional difference between those comparisons. I don't think either choice would constitute a "best practice" versus the other.