带 Switch 的简单 JavaScript 计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16422686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:35:29  来源:igfitidea点击:

Simple javascript calculator with Switch

javascripthtmlcalculator

提问by MoeSzislak

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.

我的代码不起作用,实际上没有任何响应,我没有看到任何错误,所以我可以调试......如果你在这里看到我的错误,有人能告诉我吗?我已经尝试了数百种组合,但没有调试控制台或其他东西。

采纳答案by Sirko

JavaScript does not use pointers in the way you intend to use them. You have to explicitly write back your result to the output field:

JavaScript 不会以您打算使用它们的方式使用指针。您必须明确地将结果写回输出字段:

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(). If you don't JavaScript will treat the input values as strings and then interprets +as string concatenation, for example.

此外,您应该使用parseInt()或将输入值解析为数字parseFloat()。例如,如果您不这样做,JavaScript 会将输入值视为字符串,然后解释+为字符串连接。

Example Fiddle

示例小提琴

回答by David Starkey

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

This does not allow you to set W and then have the <input/>update itself. You could do one of the following instead.

这不允许您设置 W 然后<input/>自行更新。您可以改为执行以下操作之一。

After setting wdo 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()

回答by Noel Moses Mwadende

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>