Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,255 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.