-1

so I wrote this for my first computer science class assignment. However, the page returns that if input is gpa(A), the results is 3. It's like only the first conditionals if is running. I switched around the A, B ,C and 2, 3, 4 but it's always the first if no matter what the gpa(r) is. How does this happen?

    var gpa = function(r) {
        if (r = "B"){
            return 3;
        } 
        if (r = "C"){
            return 2;
        }
        if (r = "A"){
            return 4;
        }
     }
Paco
  • 4,520
  • 3
  • 29
  • 53
whales
  • 787
  • 1
  • 12
  • 24

1 Answers1

7

In order to compare between two values you should use == or === and not = which assigns a value.

var gpa = function(r) {
    if (r == "B"){
        return 3;
    } 
    if (r == "C"){
        return 2;
    }
    if (r == "A"){
        return 4;
    }
 }

Read here about the difference between == and ===.

Community
  • 1
  • 1
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99