在javascript中检查字符串相等性

时间:2015-01-02 10:36:36

标签: javascript

我正在处理一个从html接收字符串的脚本,并检查该字符串是否与另一个字符串相等。

我遇到了一些问题。我收到一个等级从一个到一些等级被认可,有些等级不是。 问题出在D E和F. 是否可能来自==运算符的问题? 注意:我试图在第二个if语句之前提醒等级,并确保我收到一个字符串值,以便在下面的代码中出现问题。 代码:

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;
}

2 个答案:

答案 0 :(得分:4)

您的代码在尝试检查C-成绩时会中断,因为变量$course1_grade未声明。该点之后的所有代码都没有运行。

您可以通过删除该美元符号来修复您的代码,或者您可以使用查找,这将更加紧凑:

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;

您可以使用的另一种方法是switch语句:

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;

答案 1 :(得分:3)

JLRishe发现错误,看到他/她的答案,这是正确的答案。所以现在这是一个代码注释(因此使它成为CW)。

对于它的价值,我会使用查找地图或switch,而不是长if/else系列。

地图:

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>
&#13;
&#13;
&#13;

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;
}

&#13;
&#13;
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");
&#13;
<!-- 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>
&#13;
&#13;
&#13;