【问题标题】:boolean variable javascript布尔变量 javascript
【发布时间】:2013-09-18 09:24:03
【问题描述】:

您好,在如何使用“ var 破坏性变量”方面需要帮助。我需要回答 “是或否”。并在 if 中放入第三个条件

     <script type="text/javascript">

     var examResult;
     var attendance;
     var disruptive;

     examResult=parseInt(prompt("Enter Exam result:",""));
     attendance=parseInt(prompt("Enter Attendance:",""));
     disruptive=prompt("Disruptive Y/N:",""); 
     if((examResult>=40) && (attendance>=75) &&("disruptive))
     {
 document.write("You have passed");
     }
     else
     {
document.write("You have failed")
     }

     </script>

【问题讨论】:

  • if((examResult&gt;=40) &amp;&amp; (attendance&gt;=75) &amp;&amp; (disruptive !== "Y")) {...}?
  • 在您的 if 语句中有错字:双引号 "disruptive 将其删除,使其变为 disruptive

标签: javascript boolean var


【解决方案1】:

您的 if 语句有错误。去掉'"'(双引号)然后试试

【讨论】:

    【解决方案2】:

    在你的 if 语句之前添加这个

     disruptiveString=prompt("Disruptive Y/N:","");
     if (disruptiveString == "Y"){
            disruptive = true;
        }
    else{
        disruptive = false;
    }
    

    同样如 @Java Buddy 所述,删除语句中的 "

    【讨论】:

    • 无需附加条件
    【解决方案3】:

    试试这个

     var examResult;
     var attendance;
     var disruptive;
    
     examResult=parseInt(prompt("Enter Exam result:",""));
     attendance=parseInt(prompt("Enter Attendance:",""));
     disruptive=prompt("Disruptive Y/N:",""); 
     if((examResult>=40) && (attendance>=75) &&(disruptive=="Y"))
     {
        concole.log("You have passed");
     }
     else
     {
        concole.log("You have failed")
     }
    

    DEMO

    【讨论】:

    • 代替document.write(),试试document.body.innerHTML = "You have passed" - 它的破坏性较小。
    • @Pavlo:请查看我的演示,我使用的是console.log
    • 更好,但控制台消息不是普通用户会看到的。
    • @Pavlo 只是示例,用户可以根据自己的需要自定义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多