Javascript 计算器。显示 ' 。 ' 和屏幕上的操作标志
Javascript calculator. Showing ' . ' and operation sign on screen
我的计算器快完成了,但出于某种原因,我无法获得“.”。和操作标志显示在屏幕上。我的数字正在工作,计算器正在记录操作符号和点,但没有显示。按下后会显示该点并添加一个数字。正如我所说,它们都出现在控制台中。
<div class="calc-grid">
<div id="result">
<div id="prev"></div>
<div id="current"></div>
</div>
<button class='span' onClick="calculate(this)">AC</button>
<button onClick="calculate(this)">DEL</button>
<button onClick="calculate(this)">÷</button>
<button onClick="calculate(this)">1</button>
<button onClick="calculate(this)">2</button>
<button onClick="calculate(this)">3</button>
<button onClick="calculate(this)">*</button>
<button onClick="calculate(this)">4</button>
<button onClick="calculate(this)">5</button>
<button onClick="calculate(this)">6</button>
<button onClick="calculate(this)">+</button>
<button onClick="calculate(this)">7</button>
<button onClick="calculate(this)">8</button>
<button onClick="calculate(this)">9</button>
<button onClick="calculate(this)">-</button>
<button onClick="calculate(this)">.</button>
<button onClick="calculate(this)">0</button>
<button class='span' onClick="calculate(this)">=</button>
</div>
current =
prev =
lastop = ''
tempcurrent = ''
function calculate(button) {
let key = (button.textContent)
if(key == 'AC'){
current =
prev =
lastop = ''
tempcurrent = ''
}
if (key == 'DEL'){
current = current.toString().slice(0, -1)
}
if ( key >= 0 || key == '.'){
tempcurrent = tempcurrent + key
current = parseFloat(tempcurrent)
}
if ( key == '+' || key == '-' || key == '÷' || key == '*' ) {
lastop = key
prev = current
current = ''
tempcurrent = ''
}
if ( key == '=' && lastop == '+'){
current = parseFloat(prev) + parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '-'){
current = parseFloat(prev) - parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '*'){
current = parseFloat(prev) * parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '÷'){
current = parseFloat(prev) / parseFloat(current)
prev = ''
}
document.getElementById("current").innerHTML = current
document.getElementById("prev").innerHTML = prev
console.log(key)
}
您的 parseFloat()
删除了它
例如,如果我 运行 在控制台 parseFloat('1.')
它将 return 1
编辑:
您可以解决的问题:
if (key >= 0) {
tempcurrent = tempcurrent + key;
current = parseFloat(tempcurrent);
}
if (key == ".") {
tempcurrent = tempcurrent + key;
current = tempcurrent;
}
P.S。您可以使用 switch 以获得更好的代码
我的计算器快完成了,但出于某种原因,我无法获得“.”。和操作标志显示在屏幕上。我的数字正在工作,计算器正在记录操作符号和点,但没有显示。按下后会显示该点并添加一个数字。正如我所说,它们都出现在控制台中。
<div class="calc-grid">
<div id="result">
<div id="prev"></div>
<div id="current"></div>
</div>
<button class='span' onClick="calculate(this)">AC</button>
<button onClick="calculate(this)">DEL</button>
<button onClick="calculate(this)">÷</button>
<button onClick="calculate(this)">1</button>
<button onClick="calculate(this)">2</button>
<button onClick="calculate(this)">3</button>
<button onClick="calculate(this)">*</button>
<button onClick="calculate(this)">4</button>
<button onClick="calculate(this)">5</button>
<button onClick="calculate(this)">6</button>
<button onClick="calculate(this)">+</button>
<button onClick="calculate(this)">7</button>
<button onClick="calculate(this)">8</button>
<button onClick="calculate(this)">9</button>
<button onClick="calculate(this)">-</button>
<button onClick="calculate(this)">.</button>
<button onClick="calculate(this)">0</button>
<button class='span' onClick="calculate(this)">=</button>
</div>
current =
prev =
lastop = ''
tempcurrent = ''
function calculate(button) {
let key = (button.textContent)
if(key == 'AC'){
current =
prev =
lastop = ''
tempcurrent = ''
}
if (key == 'DEL'){
current = current.toString().slice(0, -1)
}
if ( key >= 0 || key == '.'){
tempcurrent = tempcurrent + key
current = parseFloat(tempcurrent)
}
if ( key == '+' || key == '-' || key == '÷' || key == '*' ) {
lastop = key
prev = current
current = ''
tempcurrent = ''
}
if ( key == '=' && lastop == '+'){
current = parseFloat(prev) + parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '-'){
current = parseFloat(prev) - parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '*'){
current = parseFloat(prev) * parseFloat(current)
prev = ''
}
if ( key == '=' && lastop == '÷'){
current = parseFloat(prev) / parseFloat(current)
prev = ''
}
document.getElementById("current").innerHTML = current
document.getElementById("prev").innerHTML = prev
console.log(key)
}
您的 parseFloat()
删除了它
例如,如果我 运行 在控制台 parseFloat('1.')
它将 return 1
编辑:
您可以解决的问题:
if (key >= 0) {
tempcurrent = tempcurrent + key;
current = parseFloat(tempcurrent);
}
if (key == ".") {
tempcurrent = tempcurrent + key;
current = tempcurrent;
}
P.S。您可以使用 switch 以获得更好的代码