简体   繁体   English

如何在不使用 eval 的情况下使用 JavaScript 的功能来创建计算器?

[英]How can I use JavaScript's functionality to create a calculator without using eval?

It's a little mess, but I need JavaScript's function to calculate.有点乱,但我需要JavaScript的函数来计算。 I avoided using eval because my teacher has not taught that.我避免使用eval因为我的老师没有教过这个。 Can help me with it?可以帮我吗?

 function A7() { var num = document.getElementById(7).value; document.getElementById("display").value = document.getElementById("display").value + num } function A8() { var num = document.getElementById(8).value; document.getElementById("display").value = document.getElementById("display").value + num } function A9() { var num = document.getElementById(9).value; document.getElementById("display").value = document.getElementById("display").value + num } function A10() { var num = document.getElementById(10).value; document.getElementById("display").value = document.getElementById("display").value + num } function A4() { var num = document.getElementById(4).value; document.getElementById("display").value = document.getElementById("display").value + num } function A5() { var num = document.getElementById(5).value; document.getElementById("display").value = document.getElementById("display").value + num } function A6() { var num = document.getElementById(6).value; document.getElementById("display").value = document.getElementById("display").value + num } function A11() { var num = document.getElementById(11).value; document.getElementById("display").value = document.getElementById("display").value + num } function A1() { var num = document.getElementById(1).value; document.getElementById("display").value = document.getElementById("display").value + num } function A2() { var num = document.getElementById(2).value; document.getElementById("display").value = document.getElementById("display").value + num } function A3() { var num = document.getElementById(3).value; document.getElementById("display").value = document.getElementById("display").value + num } function A12() { var num = document.getElementById(12).value; document.getElementById("display").value = document.getElementById("display").value + num } function A0() { var num = document.getElementById(0).value; document.getElementById("display").value = document.getElementById("display").value + num } var firstNum = getElementById(display); document.getElementById("display").value = document.getElementById("display").value + num var secondNum = getElementById(display); document.getElementById("display").value = document.getElementById("display").value + num operation = ""; function calculate() { if (operation = "+") { firstNum + secondNum; document.getElementById("display").value = firstNum + secondNum; } else if (operation = "-") { firstNum - secondNum; document.getElementById("display").value = firstNum - secondNum; } else if (operation = "/") { firstNum / secondNum; document.getElementById("display").value = firstNum / secondNum; else { firstNum * secondNum; document.getElementById("display").value = firstNum * secondNum; } }
 <BODY> <form> <TABLE> <TR> <TD COLSPAN="4"> <INPUT TYPE="text" ID="display" style="text-align:right"> </TD> </TR> <TR> <TD> <INPUT TYPE="BUTTON" VALUE="7" ID="7" onclick="A7()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="8" ID="8" onclick="A8()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="9" ID="9" onclick="A9()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="/" ID="10" onclick="A10()"> </TD> </TR> <TR> <TD> <INPUT TYPE="BUTTON" VALUE="4" ID="4" onclick="A4()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="5" ID="5" onclick="A5()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="6" ID="6" onclick="A6()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="*" ID="11" onclick="A11()"> </TD> </TR> <TD> <INPUT TYPE="BUTTON" VALUE="1" ID="1" onclick="A1()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="2" ID="2" onclick="A2()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="3" ID="3" onclick="A3()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="-" ID="12" onclick="A12()"> </TD> <TR> <TD> <INPUT TYPE="BUTTON" VALUE="0" ID="0" onclick="A0()"> </TD> <TD> <INPUT TYPE="reset" VALUE="C" ID="C"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="=" ID="14" onclick="calculate()"> </TD> <TD> <INPUT TYPE="BUTTON" VALUE="+" ID="13" onclick="A13()"> </TD> </TR> </BODY>

calculate() needs to parse the display field to get the two numbers and the operation. calculate()需要解析display字段以获取两个数字和操作。 You can do this with a simple regular expression.你可以用一个简单的正则表达式来做到这一点。

Also, you need to use == to compare strings, not = , which is for assigning.此外,您需要使用==来比较字符串,而不是用于分配的=

There's no point in writing line like像这样写行没有意义

firstNum + secondNum;

This calculates the sum but doesn't do anything with it.这会计算总和,但对它没有任何作用。 You only need the lines that assign the result of the calculation to the display value.您只需要将计算结果分配给display值的行。

 function calculate() { var input = document.getElementById("display").value.match(/^(\\d+)([+\\-*\\/])(\\d+)$/); if (!input) { alert("Nothing to calculate"); return; } var firstNum = parseInt(input[1]); var operation = input[2]; var secondNum = parseInt(input[3]); if (operation == "+") { document.getElementById("display").value = firstNum + secondNum; } else if (operation == "-") { document.getElementById("display").value = firstNum - secondNum; } else if (operation == "/") { document.getElementById("display").value = firstNum / secondNum; } else { document.getElementById("display").value = firstNum * secondNum; } }

暂无
暂无

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

相关问题 "如何在没有 eval 的 javascript 中编写计算器" - How to code a calculator in javascript without eval 有没有其他方法可以使该计算器在没有eval函数的情况下工作? - Is there an alternative way i can make this calculator work without the eval function? 如何在不使用 eval() 的情况下从字符串数组创建 JavaScript 变量 - How do I create a JavaScript variable from an array of strings without using eval() 有没有办法使用javascript创建输入/输出计算器? - Is there a way I can create an input/output calculator using javascript? 是否有一些功能可以创建没有eval的简单计算器 - Is there some function to create a simple calculator without eval 关于javascript中的eval()的问题...为什么它是邪恶的,如何在不使用它的情况下完成同样的事情? - A question about eval() in javascript… why is it evil and how can I accomplish the same thing without using it? 如何在不使用eval()的情况下从服务器动态加载和执行Javascript? - How can I dynamically load and execute Javascript from the server without using eval()? 使用HTML和Javascript评估的JavaScript计算器 - javascript calculator using HTML and Javascript eval 如何使用 JavaScript 为我的计算器项目创建一个可以添加文本的框? - How to create a box where I can add text using JavaScript for my calculator project? 如何在不复制 javascript 功能的情况下创建多个音频播放器(每个播放器包含不同的歌曲)? - How can i create multiple audio players(each containing different song) without copying javascript functionality?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM