Javascript计算器保持连接计算结果与为下一次计算输入的第一个数字

时间:2013-03-08 07:38:54

标签: javascript html

<body>
<FORM NAME="Calculator">
<TABLE BORDER=4>
<TR>
<TD>
<input type="text"   name="Input" Size="22" value="">


<br>
</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="button" NAME="one"   VALUE="1" class ="digit" >
<INPUT TYPE="button" NAME="two"   VALUE="2" class ="digit" >
<INPUT TYPE="button" NAME="three" VALUE="3" class ="digit" >
<INPUT TYPE="button" NAME="plus"  VALUE="+" class ="operand">
<br>
<INPUT TYPE="button" NAME="four"  VALUE="4" class ="digit">
<INPUT TYPE="button" NAME="five"  VALUE="5" class ="digit">
<INPUT TYPE="button" NAME="six"   VALUE="6" class ="digit">
<INPUT TYPE="button" NAME="minus" VALUE="-" class="operand">
<br>
<INPUT TYPE="button" NAME="seven" VALUE="7" class ="digit">
<INPUT TYPE="button" NAME="eight" VALUE="8" class ="digit">
<INPUT TYPE="button" NAME="nine"  VALUE="9" class ="digit">
<INPUT TYPE="button" NAME="times" VALUE="*" class ="operand">
<br>
<INPUT TYPE="button" NAME="clear" VALUE="c" class ="special">
<INPUT TYPE="button" NAME="zero"  VALUE="0" class ="digit">
<INPUT TYPE="button" NAME="Execute"  VALUE="=" class ="solve">
<INPUT TYPE="button" NAME="div"   VALUE="/" class ="operand">
<br>
</TD>
</TR>
</TABLE>
</FORM>

<script type = "text/javascript" src="C:\Users\Quonn\Desktop\QBJS\calculatorjs.js">
</script>
</body>

我正在构建一个可配置的计算器,但我正在使用我的逻辑/让它按照我想要的方式运行。我有两个问题。

问题#1:如何更改逻辑以便我可以替换“邪恶评估”?

var timer;
document.onclick = function(x) {
var info = x.target;
clearTimeout(timer);
 timer= setTimeout(function(){addDigit(x);},200);
}

问题#2 :如何更改此函数中的逻辑,以便在显示计算结果后,为下一次计算输入的第一个数字不仅仅与先前计算的结果连接?

function addDigit(x){
if (x.target.className === "digit" || x.target.className ==="operand") {
    document.Calculator.Input.value += "" + x.target.value;
}

else if (x.target.className === "solve") {
    result = eval(document.Calculator.Input.value);
    document.Calculator.Input.value = result;
}
else  {
   document.Calculator.Input.value = "";
}

}

1 个答案:

答案 0 :(得分:1)

您正在连接字符串。你应该看看parseInt / parseFloat;注意基数,否则JavaScript会试着猜测它......