Just curious about the && operator and why it doesn't work as expected. Feeling stupid here

I’m on the Practice JavaScript Syntax: Variables, Data Types, Conditionals, Etc Excercise where it’s supposed to have you write a function to get finalGrade. I’m struggling to understand why the || works and not && in the following. average >= 0 && average <= 59 doesn’t work when trying to find out if average is greater than or equal to = and average is less than or equal to 59. It just doesn’t work like that for me.

const finalGrade = (grade1, grade2, grade3) => {

  if ((grade1 < 0 || grade1 > 100) || (grade2 < 0 || grade2 > 100) || (grade3 < 0 || grade3 > 100)) {
    return 'You have entered an invalid grade.';
  }

  let average = grade1 + grade2 + grade3 / 3;

  if (average >= 0 || average <= 59) { // The || works here but not &&
    return 'F';
  } else {
    return 'BS';
  }
}

console.log(finalGrade(59, 52, 52));

If we log average:

let average = grade1 + grade2 + grade3 / 3;

console.log(average)

we see 128, which would explain the issue

seems you have forgotten to take in account math order of operation

1 Like

I noticed that and fixed the parentheses already it still doesn’t work right. I’ve solved the problem but not in the way I understand it.

Can you post an updated version of your code? Why doesn’t it work as expected?

2 Likes