3
我一直在一個簡單的JavaScript計算器超過幾天。 CSS和HTML看起來不錯。該計算器只適用於七人制和分工功能!其他數字按預期顯示在頂部輸出行中,但較低的輸出僅輸出七位數和七位數的答案。我似乎無法弄清楚爲什麼JQuery沒有輸入點擊。任何幫助,將不勝感激!這是我的鏈接到我的CodePen ... http://codepen.io/RDaniels34/pen/MKXOKx簡單的JavaScript計算器與JQuery
/*******************************
Math Functions
*******************************/
var topNumber = [];
var newnumber = [];
var operator = [];
var computedNumber = [];
/**********************************
Mathematical Function Buttons
***********************************/
function clrFunction() {
topNumber.splice(0, topNumber.length);
document.getElementById("numberDisplay1").innerHTML = 0;
computedNumber.splice(0, computedNumber.length);
document.getElementById("numberDisplay2").innerHTML = 0;
}
function divideFunction() {
topNumber[topNumber.length] = "/";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function timesFunction() {
topNumber[topNumber.length] = " X ";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function backspaceFunction() {
topNumber.splice(topNumber.length - 1, 1);
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
if (topNumber.length == []) {
document.getElementById("numberDisplay1").innerHTML = 0;
}
}
function subtractFunction() {
topNumber[topNumber.length] = " - ";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function addFunction() {
topNumber[topNumber.length] = " + ";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function percentFunction() {
topNumber[topNumber.length] = "% ";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function rd34Function() {
document.getElementById("numberDisplay1").innerHTML = "Programming is Fun!";
}
//function plusminusFunction() {
// var newNumber = "";
// if (topNumber < 0) {
// newNumber = +(topNumber);
// } else if (topNumber > 0) {
// newNumber = -(topNumber);
// }
// return plusminusFunction();
// document.getElementById("numberDisplay1").innerHTML = topNumber.splice(0, 0, newNumber);
// }
function decimalFunction() {
topNumber[topNumber.length] = ".";
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
//function equalsFunction() {
// document.getElementById("numberDisplay1").innerHTML = topNumber.join("").eval(topNumber);
// if (topNumber.length == []) {
// document.getElementById("numberDisplay2").innerHTML = 0;
// }
//}
/****************************
Number Button Functions
*****************************/
function oneFunction() {
topNumber[topNumber.length] = 1;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function twoFunction() {
topNumber[topNumber.length] = 2;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function threeFunction() {
topNumber[topNumber.length] = 3;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function fourFunction() {
topNumber[topNumber.length] = 4;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function fiveFunction() {
topNumber[topNumber.length] = 5;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function sixFunction() {
topNumber[topNumber.length] = 6;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function sevenFunction() {
topNumber[topNumber.length] = 7;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function eightFunction() {
topNumber[topNumber.length] = 8;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function nineFunction() {
topNumber[topNumber.length] = 9;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
function zeroFunction() {
topNumber[topNumber.length] = 0;
document.getElementById("numberDisplay1").innerHTML = topNumber.join("");
}
/*********************************
Bottom Number Display
********************************/
$(document).ready(function() {
var testNumLength = function(firstNumber) {
if (firstNumber.length > 9) {
$("#numberDisplay2").text(firstNumber.substr(firstNumber.length - 9, 9));
if (firstNumber.length > 15) {
firstNumber = "";
$("#numberDisplay2").text("Err");
}
}
};
var firstNumber = "";
var secondNumber = "";
var answer = "";
var operator = "";
$("#numberDisplay2").text("0");
$("#numbers").click(function() {
firstNumber += $(this).text();
$("#numberDisplay2").text(firstNumber);
testNumLength(firstNumber);
});
$("#operator").click(function() {
operator = $(this).text();
secondNumber = firstNumber;
firstNumber = "";
$("#numberDisplay2").text();
});
$("#clr").click(function() {
firstNumber = "";
$("#numberDisplay2").text("0");
if ($(this).attr("id") == "clr") {
firstNumber = "";
}
});
$("#backspace").click(function() {
firstNumber -= $(this).text();
$("#numberDisplay2").text(firstNumber);
testNumLength(firstNumber);
});
$("#equals").click(function() {
var answer = "";
parseFloat(firstNumber);
parseFloat(secondNumber);
if (operator == "-") {
answer = secondNumber - firstNumber;
} else if (operator == "X") {
answer = secondNumber * firstNumber;
} else if (operator == "/") {
answer = secondNumber/firstNumber;
} else if (operator == "+") {
answer = secondNumber + firstNumber;
}
// else if (operator == "%") {
// solution = number/100;
// }
//answer.toString("");
$("#numberDisplay2").text(answer.toFixed(6));
testNumLength(answer);
//newNumber = "";
//nextNumber = "";
});
});
謝謝達倫!這對現在兩條線都有效......現在我需要努力讓它輸出正確的答案......我現在只是拿NaN作爲答案......我的「Sevens Division Calculator」現在不工作......呵呵......再次感謝! – RDaniels34
達倫,現在就爲所有人工作吧!還有其他的功能可以使用,但是我可以使用我可以使用的數字!你是一個救星! – RDaniels34