【问题标题】:Javascript if statements not working [duplicate]Javascript if 语句不起作用[重复]
【发布时间】:2023-02-06 20:15:32
【问题描述】:

非常简单明了我想做什么:

  • 如果输入的是0,说明他们没有输入数字 应该告诉你。
  • 当输入7时,它应该说你做对了。
  • 任何其他的,它应该告诉你你弄错了。

但无论输入是什么,它都只输出“7 is correct”行,我无法弄清楚哪里出了问题。

<script type="text/javascript">
function problem2 ()
{
var number = 0;
var text=document.getElementById("output");
number = prompt("Enter a number between 1 and 10 please" , 0);
if (number = 0)
    {
     text.value = "You didn't enter a number!";
    }
if (number = 7)
    {
     text.value = "7 is correct!";
    }
else
    {
     text.value = "Sorry, ", input, "is not correct!";
    }
}
</script>
<input type="button" value="Click here" onclick="problem2()">
<input id="output" type="text">

【问题讨论】:

    标签: javascript if-statement


    【解决方案1】:

    您正在分配 =。使用=====

    if( 0 == number ){
    
      text.value = "You didn't enter a number!";
    }
    

    另外,请注意您的牙套放置。 Javascript 喜欢在行尾自动添加分号。 Source

    【讨论】:

    • 除非您确切知道为什么要使用 ==,否则您应该更喜欢不会强制类型的 ===。另见:this awesome stackoverflow answer on the subject
    • 在上面的示例中使用=== 将不起作用,因为prompt() 的返回值是一个字符串。你需要使用if(number === "7")...
    【解决方案2】:

    您正在使用赋值运算符作为条件而不是比较运算符:

    if (number = 0) // falsy. Same as if (false)
        {
         text.value = "You didn't enter a number!";
        }
    if (number = 7) // truthy. Same as if (true)
        {
         text.value = "7 is correct!";
        }
    else
        {
         text.value = "Sorry, ", input, "is not correct!";
        }
    

    或者,您可以使用开关并更轻松地组织条件:

    switch (number) {
        case 0: 
            text.value = "You didn't enter a number!";
            break;
    
        case 7:
            text.value = "7 is correct!";
            break;
    
        default:
            text.value = "Sorry, ", input, "is not correct!";
            break;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个包含一些修复和改进的代码(我评论了我所做的更改):

      function problem2 (){
          //I multiplied by * 1 to work with numbers, also used || to default to 0 in case of NaN
          var num = (prompt("Enter a number between 1 and 10 please" , 0) * 1) || 0;
          var msg = "";
      
          if (!num){ //I prefer this over 'num == 0'
               msg = "You didn't enter a number!";
          //you should use 'else if' in this case
          }else if (num == 7){//'=' is for assignment, use '==' or '===' instead
               msg = "7 is correct!";
          }else{
              //you had an undefined var 'input', you probably meant 'num'
              //you also were connecting var and strings using commas, use '+' instead
               msg = "Sorry, " + num + " is not correct!"; //added a space in ' is'
          }
      
          //no need to store the element in a var anymore :D
          document.getElementById("output").value = msg;
      }
      

      此外,还可以进行两个更改:

      • 只有一个var(例如var something = "", somethingElse = 99;
      • 从头分配默认文本,如var msg = "default"并删除else

      笔记:我做的一个未记录的更改是重命名一些变量,我鼓励大家停止使用像number, text, string这样的变量,如果你有这个坏习惯,你最终会错误地使用非法变量名。

      【讨论】:

        猜你喜欢
        • 2018-08-28
        • 2012-11-30
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多