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

Is my solution really invalid?

The challenge says this is wrong, but how so?

   get level() { 
       if (this.credits > 90) {
          return 'Senior';
       } else if ( 60 < this.credits <= 90) {
          return 'Junior';
       } else if (30 < this.credits <= 60) {
          return 'Sophomore';
       } else if (this.credits <= 30) {
          return 'Freshman';
       }
  }    

I solved it by putting in &&'s and repeating this.credits, but I thought my way would have been just as valid, and cleaner. Can someone clarify? Thanks.

1 Answer

From the second answer from this stack overflow:

Given:

if(x > y > z)

Javascript will try to parse your original statement from left to right and you'll end up comparing z to a boolean value which will then be parsed to a number (0 or 1).

So your original statement is equivalent to

if( (x > y && 1 > z) || (x <= y && 0 > z))

For your code:

else if ( 60 < this.credits <= 90)

60 < this.credits will evaluate to 1 or 0 which are less than or equal to 90. So any number less than 91 will return 'Junior'

Perfect, thank you kindly.