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 Writing the JavaScript Code

Jason Hill
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Hill
Full Stack JavaScript Techdegree Student 11,399 Points

Help me understand this logic difference, and why it does not work. Translating Pseudocode to a Function

So I am working through this example to translate pseudocode and make a function that calculates GPA, grades being passed are all between 1-4, and I had everything right except my code was returning invalid grade. After viewing the solution video, I see Dave uses the logic:

if (grade < 1 || grade > 4) {
      console.log("Invalid Grade " + student_grades[i]);
      return console.log("cannot complete calculation");

However, my original idea behind this function was to use the following and it fails, but to me it makes sense. I am not sure why it fails and just would like to understand. All grades being passed are between 1 and 4, and it tells me invalid. I do see now Dave's is more eloquent than mine of course, but given only 4 values to compare I first went with this.

if (grade !== 1 || 2 || 3 || 4){
      console.log("Invalid Grade " + student_grades[i]);
      return console.log("cannot complete calculation");

I may not be understanding the logical operator correctly I assume, as to me it seems like it should compare, between 1-4, and if not return, otherwise continue. Thanks!

2 Answers

Steven Parker
Steven Parker
231,007 Points

You can only combine complete comparison expressions with logic. Also, when combining inequality you will need the AND operator instead of OR:

if (grade !== 1 && grade !== 2 && grade !== 3 && grade !== 4) {