如何让等号在JavaScript中重复计算?
How to make equal sign do repetitive calculation in JavaScript?
简要说明:我正在尝试使用 html 和 JavaScript 制作一个基本计算器。到目前为止,我已经使用数字、运算符、小数和等号键(按钮)成功完成了计算器的所有基本功能。
问题:我试图通过在操作完成后多次按等号来实现它,它将执行最后一个操作。比如下面的例子:
运算:5+6-3
答案(等号按一次):8
按两次等号:8-3 = 5(我的代码中没有出现这种情况,似乎无法找出原因)
这种情况下的最后一个操作是“-3”
代码如下,我使用了大部分函数和不会导致问题的代码位,因为我的代码大约有 200 行。 (以下代码约80行)
代码:
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var input;
var inputValue;
var buttonValue;
var equalOnce = false;
var decimalAdd = false;
var lastOperation;
readIn(keys, input, inputValue,buttonValue);
function readIn(keys, input, inputValue, key) {
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
input = document.querySelector('.screen');
inputValue = input.innerHTML;
key = this.innerHTML;
keyPressed(key, inputValue, input);
}
}
}
//clears screen and input
function clearScreen(input){}
//calculates all math functions within the equation.
function evalulated(lastCharacter, equations, operator, input){
//replaces x with * and ÷ with /
equations = equations.replace(/x/g, '*').replace(/÷/g, '/');
//checks last character and removes if its an decimal or operator
if(operator.indexOf(lastCharacter) > -1 || lastCharacter == '.')
equations = equations.replace(/.$/, '');
if(equations){
input.innerHTML = eval(equations);
}
return;
}
//finds the last operator and take the operator and the last digits/number used with the operrator and save it into lastOperation
function findLastEquation(equation, lastOperation){
var plusLastIndex = equation.lastIndexOf('+');
var subLastIndex = equation.lastIndexOf('-');
var divideLastIndex = equation.lastIndexOf('÷');
var multipleLastIndex = equation.lastIndexOf('x');
if(plusLastIndex > subLastIndex && plusLastIndex > divideLastIndex && plusLastIndex > multipleLastIndex){
lastOperation = equation.slice(plusLastIndex, equation.length);
}else if(subLastIndex > plusLastIndex && subLastIndex > divideLastIndex && subLastIndex > multipleLastIndex){
lastOperation = equation.slice(subLastIndex, equation.length);
} else if(divideLastIndex > plusLastIndex && divideLastIndex > subLastIndex && divideLastIndex > multipleLastIndex){
lastOperation = equation.slice(divideLastIndex, equation.length);
} else if(multipleLastIndex > plusLastIndex && multipleLastIndex > subLastIndex && multipleLastIndex > divideLastIndex){
lastOperation = equation.slice(multipleLastIndex, equation.length);
}else{
lastOperation = 0;
}
return lastOperation;
}
function keyPressed(key, inputValue, input) {
var equation;
var lastChar;
var newEquation;
if(key == 'C') {
clearScreen(input);
}
else if(key == '=') {
if(equalOnce == false) {
equalOnce = true;
equation = inputValue;
lastOperation = findLastEquation(equation, lastOperation);
}else {
equation = equation.concat(lastOperation);
alert(equation);
}
lastChar = equation[equation.length - 1];
evalulated(lastChar, equation, operators, input);
decimalAdd = false;
}
else if(operators.indexOf(key) > -1) {
lastChar = inputValue[inputValue.length - 1];
if(operators.indexOf(lastChar) > -1 && inputValue.length > 1) {}
equalOnce = false;
}
//adds a decimal to input, adds a '0.' if no integer was before it
else if(key == '.' && equalOnce == false) {
if(decimalAdd == false) {}
input.innerHTML += key;
decimalAdd = true;
}
equalOnce = false;
}
else {
if(equalOnce == true){
clearScreen(input);
}
if(key == '.'){}
input.innerHTML += key;
}
}
任何帮助或建议都将非常有用!提前致谢!
我忘了写声明:
equation = inputValue;
在 if(key == '='){} 中的 if else 语句之外
现在工作正常。
简要说明:我正在尝试使用 html 和 JavaScript 制作一个基本计算器。到目前为止,我已经使用数字、运算符、小数和等号键(按钮)成功完成了计算器的所有基本功能。
问题:我试图通过在操作完成后多次按等号来实现它,它将执行最后一个操作。比如下面的例子:
运算:5+6-3
答案(等号按一次):8
按两次等号:8-3 = 5(我的代码中没有出现这种情况,似乎无法找出原因)
这种情况下的最后一个操作是“-3”
代码如下,我使用了大部分函数和不会导致问题的代码位,因为我的代码大约有 200 行。 (以下代码约80行)
代码:
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var input;
var inputValue;
var buttonValue;
var equalOnce = false;
var decimalAdd = false;
var lastOperation;
readIn(keys, input, inputValue,buttonValue);
function readIn(keys, input, inputValue, key) {
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
input = document.querySelector('.screen');
inputValue = input.innerHTML;
key = this.innerHTML;
keyPressed(key, inputValue, input);
}
}
}
//clears screen and input
function clearScreen(input){}
//calculates all math functions within the equation.
function evalulated(lastCharacter, equations, operator, input){
//replaces x with * and ÷ with /
equations = equations.replace(/x/g, '*').replace(/÷/g, '/');
//checks last character and removes if its an decimal or operator
if(operator.indexOf(lastCharacter) > -1 || lastCharacter == '.')
equations = equations.replace(/.$/, '');
if(equations){
input.innerHTML = eval(equations);
}
return;
}
//finds the last operator and take the operator and the last digits/number used with the operrator and save it into lastOperation
function findLastEquation(equation, lastOperation){
var plusLastIndex = equation.lastIndexOf('+');
var subLastIndex = equation.lastIndexOf('-');
var divideLastIndex = equation.lastIndexOf('÷');
var multipleLastIndex = equation.lastIndexOf('x');
if(plusLastIndex > subLastIndex && plusLastIndex > divideLastIndex && plusLastIndex > multipleLastIndex){
lastOperation = equation.slice(plusLastIndex, equation.length);
}else if(subLastIndex > plusLastIndex && subLastIndex > divideLastIndex && subLastIndex > multipleLastIndex){
lastOperation = equation.slice(subLastIndex, equation.length);
} else if(divideLastIndex > plusLastIndex && divideLastIndex > subLastIndex && divideLastIndex > multipleLastIndex){
lastOperation = equation.slice(divideLastIndex, equation.length);
} else if(multipleLastIndex > plusLastIndex && multipleLastIndex > subLastIndex && multipleLastIndex > divideLastIndex){
lastOperation = equation.slice(multipleLastIndex, equation.length);
}else{
lastOperation = 0;
}
return lastOperation;
}
function keyPressed(key, inputValue, input) {
var equation;
var lastChar;
var newEquation;
if(key == 'C') {
clearScreen(input);
}
else if(key == '=') {
if(equalOnce == false) {
equalOnce = true;
equation = inputValue;
lastOperation = findLastEquation(equation, lastOperation);
}else {
equation = equation.concat(lastOperation);
alert(equation);
}
lastChar = equation[equation.length - 1];
evalulated(lastChar, equation, operators, input);
decimalAdd = false;
}
else if(operators.indexOf(key) > -1) {
lastChar = inputValue[inputValue.length - 1];
if(operators.indexOf(lastChar) > -1 && inputValue.length > 1) {}
equalOnce = false;
}
//adds a decimal to input, adds a '0.' if no integer was before it
else if(key == '.' && equalOnce == false) {
if(decimalAdd == false) {}
input.innerHTML += key;
decimalAdd = true;
}
equalOnce = false;
}
else {
if(equalOnce == true){
clearScreen(input);
}
if(key == '.'){}
input.innerHTML += key;
}
}
任何帮助或建议都将非常有用!提前致谢!
我忘了写声明:
equation = inputValue;
在 if(key == '='){} 中的 if else 语句之外 现在工作正常。