【问题标题】:Why Is my code not outputting the string from the if statement?为什么我的代码没有从 if 语句中输出字符串?
【发布时间】:2018-01-29 00:49:19
【问题描述】:

这是我的代码: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

【问题讨论】:

  • 你检查过demlean.WA不是undefined吗?
  • 欢迎来到 Stack Overflow。你熟悉the switch statement吗?
  • 对于初学者,您永远不会将 demlean.WA 设置为任何内容。当然,还有很多更好的方法来构建它。
  • 这么少的代码中有这么多var lWA

标签: javascript if-statement output


【解决方案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

【讨论】:

    【解决方案2】:

    在您发布的代码中,最后缺少一个结束 }

    【讨论】:

      【解决方案3】:

      问题

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

      解决方案

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

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

      看到了吗?现在您的代码按预期运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        相关资源
        最近更新 更多