简体   繁体   中英

Checking string equality in javascript

I'm working on a script that receives a string from html and checks whether the string equal another string.

I have a problem with some stings. I receive a grade from a to f some grades are recognized and some grades are not. the problem is with DE and F. is it possible that the problem from the == operator? Note: I tried to alert the grade before the second if statement and make sure that I receive a string value so the problem in the following code. code:

if (course1_grade) {
    if (course1_grade == "A") {
        course1_point = 4.0 * course1_credit;
    } else if (course1_grade == "A-") {
        course1_point = 3.67 * course1_credit;
    } else if (course1_grade == "B+") {
        course1_point = 3.33 * course1_credit;
    } else if (course1_grade == "B") {
        course1_point = 3.0 * course1_credit;
    } else if (course1_grade == "B-") {
        course1_point = 2.67 * course1_credit;
    } else if (course1_grade == "C+") {
        course1_point = 2.33 * course1_credit;
    } else if (course1_grade == "C") {
        course1_point = 2.0 * course1_credit;
    } else if ($course1_grade == "C-") {
        course1_point = 1.67 * course1_credit;
    } else if (course1_grade == "D+") {
        course1_point = 1.33 * course1_credit;
    } else if (course1_grade == "D") {
        course1_point = 1.0 * course1_credit;
    } else if (course1_grade == "F") {
        course1_point = 0.0 * course1_credit;
    }
} else {
    course1_point = 0.0;
}

Your code is breaking when it tries to check for a C- grade because the variable $course1_grade is undeclared. None of the code after that point runs.

You can fix your code by removing that dollar sign, or you could use a lookup, which would be much more compact:

var gradeLookup = {
    "A" : 4,
    "A-": 3.67,
    "B+": 3.33,
    "B" : 3,
    "B-": 2.67,
    "C+": 2.33,
    "C" : 2,
    "C-": 1.67,
    "D+": 1.33,
    "D" : 1
};

course1_grade = (course1_grade || "").trim().toUpperCase();
course1_point = (gradeLookup[course1_grade] || 0) * course1_credit;

Another approach you can use is a switch statement:

function getFactor(grade) {
    switch ((grade || "").trim().toUpperCase()) {
        case "A" : return 4.0;
        case "A-": return 3.67;
        case "B+": return 3.33;
        case "B" : return 3;
        case "B-": return 2.67;
        case "C+": return 2.33;
        case "C" : return 2;
        case "C-": return 1.67;
        case "D+": return 1.33;
        case "D" : return 1;
        default  : return 0;
    }
}

course1_point = getFactor(course1_grade) * course1_credit;

JLRishe spotted the error, see his/her answer, which is the correct one. So this is now a comment with code in (hence making it a CW).

For what it's worth, I'd use a lookup map or a switch , not a long if/else series.

Map:

var factor = {
  "A":  4.0,
  "A-": 3.67,
  "B+": 3.33,
  "B":  3.0,
  "B-": 2.67,
  "C+": 2.33,
  "C":  2.0,
  "C-": 1.67,
  "D+": 1.33,
  "D":  1.0,
  "F":  0.0
};
course1_point = (factor[course1_grade.trim().toUpperCase()] || 0.0) * course1_credit;

 var factor = { "A": 4.0, "A-": 3.67, "B+": 3.33, "B": 3.0, "B-": 2.67, "C+": 2.33, "C": 2.0, "C-": 1.67, "D+": 1.33, "D": 1.0, "F": 0.0 }; var course1_credit = 100; function test(course1_grade) { var course1_point = (factor[course1_grade.trim().toUpperCase()] || 0.0) * course1_credit; snippet.log("grade = " + course1_grade + ", points = " + course1_point); } test("a"); test("a-"); test("b+"); test("b"); test("b-"); test("c+"); test("C"); test("C-"); test("D+"); test("D"); test("F"); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

switch :

switch (course1_grade.trim().toUpperCase()) {
  case "A":
    course1_point = 4.0 * course1_credit;
    break;
  case "A-":
    course1_point = 3.67 * course1_credit;
    break;
  case "B+":
    course1_point = 3.33 * course1_credit;
    break;
  case "B":
    course1_point = 3.0 * course1_credit;
    break;
  case "B-":
    course1_point = 2.67 * course1_credit;
    break;
  case "C+":
    course1_point = 2.33 * course1_credit;
    break;
  case "C":
    course1_point = 2.0 * course1_credit;
    break;
  case "C-":
    course1_point = 1.67 * course1_credit;
    break;
  case "D+":
    course1_point = 1.33 * course1_credit;
    break;
  case "D":
    course1_point = 1.0 * course1_credit;
    break;
  case "F":
    course1_point = 0.0 * course1_credit;
    break;
  default:
    course1_point = 0.0;
}

 var course1_credit = 100; function test(course1_grade) { var course1_point; switch (course1_grade.trim().toUpperCase()) { case "A": course1_point = 4.0 * course1_credit; break; case "A-": course1_point = 3.67 * course1_credit; break; case "B+": course1_point = 3.33 * course1_credit; break; case "B": course1_point = 3.0 * course1_credit; break; case "B-": course1_point = 2.67 * course1_credit; break; case "C+": course1_point = 2.33 * course1_credit; break; case "C": course1_point = 2.0 * course1_credit; break; case "C-": course1_point = 1.67 * course1_credit; break; case "D+": course1_point = 1.33 * course1_credit; break; case "D": course1_point = 1.0 * course1_credit; break; case "F": course1_point = 0.0 * course1_credit; break; default: course1_point = 0.0; } snippet.log("grade = " + course1_grade + ", points = " + course1_point); } test("a"); test("a-"); test("b+"); test("b"); test("b-"); test("c+"); test("C"); test("C-"); test("D+"); test("D"); test("F"); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM