简体   繁体   English

带 Switch 的简单 JavaScript 计算器

[英]Simple javascript calculator with Switch

I need some help with a simple calculator I am doing with javascript.我需要一些我正在用 javascript 做的简单计算器的帮助。

The js code is as follows (my teacher want's me to use 4 functions, for each kind of operation): js代码如下(老师要我用4个函数,每种操作):

<script>

            function plus(a,b) {
                return (a + b);
            }

            function minus(a,b) {
                return (a - b);
            }

            function multiply(a,b) {
                return (a * b);
            }

            function divide(a,b) {
                return (a / b);
            }

            function calc() {

                var x = document.getElementById("oper1").value;
                var y = document.getElementById("operx").value;
                var z = document.getElementById("oper2").value;
                var w = document.getElementById("resul").value;

                switch (y) {
                    case '0':
                        w = plus(x, z);
                        break;

                    case '1':
                        w = minus(x, z);
                        break;

                    case '2':
                        w = multiply(x, z);
                        break;

                    case '3':
                        w = divide(x, z);
                        break;

                    default:
                        w = "Don't really know..";
                }

            }

</script>
<input type="text" id="oper1" value="">

<select id="operx">
    <option value="0">SUMAR</option>
    <option value="1">RESTAR</option>
    <option value="2">MULTIPLICAR</option>
    <option value="3">DIVIDIR</option>
</select>

<input type="text" id="oper2" value="">
<input type="button" onClick="calc();" value="=">
<input type="text" id="resul" value="">

My code isn't working, in fact is not responding anything and I don't see any errors so I can debug... could anyone tell me if you see my mistake here?我的代码不起作用,实际上没有任何响应,我没有看到任何错误,所以我可以调试......如果你在这里看到我的错误,有人能告诉我吗? I've tried hundreds of combinations but without having an debug console or something.我已经尝试了数百种组合,但没有调试控制台或其他东西。

var w = document.getElementById("resul").value;

This does not allow you to set W and then have the <input/> update itself.这不允许您设置 W 然后让<input/>更新本身。 You could do one of the following instead.您可以改为执行以下操作之一。

After setting w do document.getElementById("resul").value=w;设置wdocument.getElementById("resul").value=w;

OR要么

var w = document.getElementById("resul");
w.value=etc.

Also, bonus points if you validate your form (nice function for this case is IsNumeric() )此外,如果您验证表单,则IsNumeric()奖励积分(这种情况下的好函数是IsNumeric()

JavaScript does not use pointers in the way you intend to use them. JavaScript 不会以您打算使用它们的方式使用指针。 You have to explicitly write back your result to the output field:您必须明确地将结果写回输出字段:

function calc(){
  // most of your stuff
  // just replace the var w = document ... line with just
  var w;

  // your other code

  document.getElementById("resul").value = w;
}

Furthermore, you should parse the input values into a number using either parseInt() or parseFloat() .此外,您应该使用parseInt()parseFloat()将输入值解析为数字。 If you don't JavaScript will treat the input values as strings and then interprets + as string concatenation, for example.例如,如果您不这样做,JavaScript 会将输入值视为字符串,然后将+解释为字符串连接。

Example Fiddle示例小提琴

 // You can simply add button to call a function like this

 <button onclick="Calculator()">=</button>

functon Calculator(){
 var x = document.getElementById("oper1").value;
 var y = document.getElementById("operx").value;
 var z = document.getElementById("oper2").value;
           
switch(y){
case "+":
document.getElementById("resul") = x + z;
break;
case "-":
document.getElementById("resul") = x - z;
break;

case "*":
document.getElementById("resul") = x * z;
break;

case "/":
document.getElementById("resul") = x / z;
break;

case "%":
document.getElementById("resul") = x % z;
break;

Default:
alert("Choose the operator");

}
}

Your program should look like this你的程序应该是这样的

 <script> function plus(a,b) { return (a + b); } function minus(a,b) { return (a - b); } function multiply(a,b) { return (a * b); } function divide(a,b) { return (a / b); } function calc() { var w; var x = document.getElementById("oper1").value; var y = document.getElementById("operx").value; var z = document.getElementById("oper2").value; //document.getElementById("resul").innerHTML=w; switch (y) { case '0': w = plus(x, z); document.getElementById("resul").innerHTML=w; break; case '1': w = minus(x, z); document.getElementById("resul").innerHTML=w; break; case '2': w = multiply(x, z); document.getElementById("resul").innerHTML=w; break; case '3': w = divide(x, z); document.getElementById("resul").innerHTML=w; break; default: w = "Don't really know.."; } } </script> <input type="text" id="oper1" value=""> <select id="operx"> <option value="0">SUMAR</option> <option value="1">RESTAR</option> <option value="2">MULTIPLICAR</option> <option value="3">DIVIDIR</option> </select> <input type="text" id="oper2" value=""> <input type="button" onClick="calc();" value="Solve"><br/><br/> <p id="resul">The answer is : </p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM