首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在JavaScript中使用Conditionals打印消息

在JavaScript中使用Conditionals打印消息
EN

Stack Overflow用户
提问于 2018-06-23 03:35:18
回答 3查看 172关注 0票数 0

我用HTML和JavaScipt创建了一个计算器。计算器能用,所以没问题。但是,如果结果为NaN,我想在html中编写一条消息,让用户知道他们必须输入一个变量。虽然我知道我需要使用条件语句,但我不确定如何编写它。

下面是我的代码:

代码语言:javascript
复制
function calc(){
        var n1 = parseFloat(document.getElementById("n1").value);
        var n2 = parseFloat(document.getElementById("n2").value);
        var oper = document.getElementById("operators").value;

        if( oper === "+"){
            document.getElementById("result").value = n1+n2;
        }
        if( oper === "-"){
            document.getElementById("result").value = n1-n2;
        }
        if( oper === "*"){
            document.getElementById("result").value = n1*n2;
        }
        if( oper === "/"){
            document.getElementById("result").value = n1/n2;
        }
        if( oper === NaN ){
            document.getElementById("Comments").innerHTML= "Write something in the boxes, you silly ass." ;
        }
     }
代码语言:javascript
复制
<input type="text" id="n1"/><br/><br/>
<input type="text" id="n2"/><br/><br> 

<select id="operators">
    <option value="+">+</option> 
    <option value="-">-</option> 
    <option value="X">X</option> 
    <option value="/">/</option> 
</select>

<input type="text" id="result"/> 
 <button onclick="calc();">=</button>
<p id="Comments"></p>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-23 03:47:26

要改进代码,您可以添加else ifisNaN,如下所示:

代码语言:javascript
复制
function calc(){
        var n1 = parseFloat(document.getElementById("n1").value);
        var n2 = parseFloat(document.getElementById("n2").value);
        var oper = document.getElementById("operators").value;

        if( oper === "+"){
            document.getElementById("result").value = n1+n2;
        } else if( oper === "-"){
            document.getElementById("result").value = n1-n2;
        } else if( oper === "*"){
            document.getElementById("result").value = n1*n2;
        } else if( oper === "/"){
            document.getElementById("result").value = n1/n2;
        } else if( isNaN(oper) ){
            document.getElementById("Comments").innerHTML= "Write something in the boxes, you silly ass." ;
        }
     }
代码语言:javascript
复制
<input type="text" id="n1"/><br/><br/>
<input type="text" id="n2"/><br/><br> 

<select id="operators">
    <option value="+">+</option> 
    <option value="-">-</option> 
    <option value="X">X</option> 
    <option value="/">/</option> 
</select>

<input type="text" id="result"/> 
 <button onclick="calc();">=</button>
<p id="Comments"></p>

票数 1
EN

Stack Overflow用户

发布于 2018-06-23 03:43:51

使用isNaN函数进行检查

代码语言:javascript
复制
    function calc(){
        var n1 = parseFloat(document.getElementById("n1").value);
        var n2 = parseFloat(document.getElementById("n2").value);
        var oper = document.getElementById("operators").value;

        if( oper === "+"){
            document.getElementById("result").value = n1+n2;
        }
        if( oper === "-"){
            document.getElementById("result").value = n1-n2;
        }
        if( oper === "*"){
            document.getElementById("result").value = n1*n2;
        }
        if( oper === "/"){
            document.getElementById("result").value = n1/n2;
        }
        if( isNaN(oper) ){
            document.getElementById("Comments").innerHTML= "Write something in the boxes, you silly ass." ;
        }
     }
代码语言:javascript
复制
<input type="text" id="n1"/><br/><br/>
<input type="text" id="n2"/><br/><br> 

<select id="operators">
    <option value="+">+</option> 
    <option value="-">-</option> 
    <option value="X">X</option> 
    <option value="/">/</option> 
</select>

<input type="text" id="result"/> 
 <button onclick="calc();">=</button>
<p id="Comments"></p> 

票数 1
EN

Stack Overflow用户

发布于 2018-06-23 04:49:28

我将提供一个更简明的版本。如果您打算稍后更改一个元素,则没有理由多次选择该元素。使用对象作为选项的容器允许您更容易地验证和执行。

代码语言:javascript
复制
const options = {
  '+'(a, b) { return a + b },
  '-'(a, b) { return a - b },
  '*'(a, b) { return a * b },
  '/'(a, b) { return a / b }
};
function calc(){
  var n1 = parseFloat(document.getElementById("n1").value);
  var n2 = parseFloat(document.getElementById("n2").value);
  var oper = document.getElementById("operators").value;
  const resEl = document.getElementById("result");
  resEl.value = '';
  const comEl = document.getElementById("Comments");
  comEl.innerHTML = '';

  let result;
  let targetEl;

  if (isNaN(n1) || isNaN(n2)) {
    targetEl = comEl;
    result = "Write something valid in the boxes, silly.";
  }
  else if (options[oper]) {
    target = resEl;
    result = options[oper](n1, n2);
  }
  else {
    targetEl = comEl;
    result = "Pick an operation." ;
  }

  let prop = typeof result === 'string' ? 'innerHTML' : 'value';
  targetEl[prop] = result;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50994559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档