0
我的計算器一次只能操作2個數字。我不知道如何在按=之前用不同的操作符連接兩個以上的數字。任何人都可以提供一個如何去做的暗示嗎? (下面是我的javascript代碼)鏈接在基本的計算器?
$(document).ready(function(){
var testNumLength = function(number) {
if (number.length > 9) {
totaldiv.text(number.substr(number.length-9,9));
if (number.length > 15) {
number = "";
totaldiv.text("Err");
}
}
};
var entry = "";
var current = ""; //after operator is entered
var operator = "";
var res = "";
var totaldiv = $("#total");
totaldiv.text("0");
$("#numbers a").not("#clear,#clearall").click(function(){
entry += $(this).text();//take the text of the numbers when clicked and append it to var entry
//display input1 on screen
totaldiv.html(entry);
testNumLength(entry);
})
$("#clear,#clearall").click(function(){
entry = "";
if ($(this).attr("id") === "clearall"){
current = "";
}
totaldiv.text("0");
})
$("#operators a").click(function(){
//append operators to var operator
operator = $(this).text();
current = entry;
entry = "";
})
$("#decimal").click(function(){
//var numOfDecs = 0;
for (var i=0;i<entry.length;i++){
if (entry.indexOf(".")== -1){
entry += ".";
// numOfDecs += 1;
}
}
totaldiv.text(entry);
testNumLength(entry);
})
$("#equals").click(function(){
var result = eval(current + operator + entry);
entry = result;
totaldiv.html(result.toFixed(2));
})
})