为什么我的代码没有从if语句输出字符串?

时间:2018-01-29 00:49:20

标签: javascript if-statement output

以下是我的代码:https://plnkr.co/edit/HFyKq2JZipwAAST0iNAt?p=preview 为什么不将if语句的结果输​​出到ID为“WA”的标记 以下是与上面链接的代码分开的IF语句:

if (demlean.WA <= 5 && demlean.WA >= -5) {
        if (demlean.WA > 0) {
          var lWA = "Tossup, Tilt D";
        } else if (demlean.WA < 0) {
          var lWA = "Tossup, Tilt R";
        } else {
          var lWA = "Absolute Tossup";
        }

      } else if (demlean.WA > 5) {
        if (demlean.WA <= 10) {
          var lWA = "Lean D";
        } else if (demlean.WA <= 17) {
          var lWA = "Likely D";
        } else {
          var lWA = "Safe D";
        }
      } else {
        if (demlean.WA >= -10) {
          var lWA = "Lean R";
        } else if (demlean.WA >= -17) {
          var lWA = "Likely R";
        } else {
          var lWA = "Safe R";
        }
 }
.... // more code

3 个答案:

答案 0 :(得分:1)

好的,最后我能够运行你的代码,这是我所做的修改。 首先,

Uncaught ReferenceError: dDe is not defined
    at calc ((index):140)
    at submit ((index):153)
    at HTMLButtonElement.onclick ((index):167)

所以我将这一行改为DE: dDE,。然后是另一个错误

Uncaught ReferenceError: demlean is not defined
    at lean ((index):45)
    at calc ((index):146)
    at submit ((index):153)
    at HTMLButtonElement.onclick ((index):167)

demlean是一个局部变量。所以我需要把它传递给精益功能。

    .....
    var geba;
    var tda;
    /* If Statement for determing lean in variable lSTATE */
    function lean(demlean) {
    .....
    .
    .
    .
    DE: dDE,
    MD: dMD,
    NY: dNY,
    VT: dVT,
    ME: dME,
    HI: dHI
  };
  // passing demlean here.
  lean(demlean);

希望这有帮助。以下是更新后的代码链接:https://plnkr.co/edit/fGlSmDEWNEtKEZ7sLgUC?p=info

答案 1 :(得分:0)

在您发布的代码中,最后缺少结尾}

答案 2 :(得分:0)

问题

  • 缺少最后一个结束花括号: - )

解决方案

  • 添加缺少的大括号: - )

var demlean = {
  WA: 5
};

if (demlean.WA <= 5 && demlean.WA >= -5) {
  if (demlean.WA > 0) {
    var lWA = "Tossup, Tilt D";
  } else if (demlean.WA < 0) {
    var lWA = "Tossup, Tilt R";
  } else {
    var lWA = "Absolute Tossup";
  }

} else if (demlean.WA > 5) {
  if (demlean.WA <= 10) {
    var lWA = "Lean D";
  } else if (demlean.WA <= 17) {
    var lWA = "Likely D";
  } else {
    var lWA = "Safe D";
  }
} else {
  if (demlean.WA >= -10) {
    var lWA = "Lean R";
  } else if (demlean.WA >= -17) {
    var lWA = "Likely R";
  } else {
    var lWA = "Safe R";
  }
}

console.log(lWA);

请参阅?现在你的代码按预期运行。