我希望對以下問題有任何幫助。當我在我的計算器中輸入我的字符串時,它顯示Nan,下一個整數變化-1 + 2到-1 + -2,它顯示Nan。我使用Math.abs()將我的值更改爲負值或正值。無法獲得加號/減號按鈕以正常工作javascript
var infoBox = document.getElementById('screen');
function addscreen(x) { //x verteenwoordig die knopie wat gedruk word bv 1 en dan by vorige waarde gevoeg word van infoBox;
infoBox.value = infoBox.value + x;
if (x === 'C') {
infoBox.value = '';
}
}
function changesign(x) {
changeNum();
}
function changeNum(x) {
x = infoBox.value;
x = Math.sign(x);
if (x === 1) {
x = infoBox.value;
x = -Math.abs(x);
infoBox.value = x;
} else {
x = infoBox.value;
x = Math.abs(x);
infoBox.value = x;
}
}
function product(x) {
x = infoBox.value;
x = eval(x);
infoBox.value = x;
}
function mutiplyBy() {
x = infoBox.value;
x = eval(x * x);
infoBox.value = x;
}
function backspace() {
var character = infoBox.value;
var size = character.length - 1;
var newCharacter = character.substring(0, size);
infoBox.value = newCharacter;
}
function mutiplyBy2(x) {
x = infoBox.value;
x = x/100;
infoBox.value = x;
}
/*function mutiplyBy2 (x){
\t x = infoBox.value;
\t x = x/100 * 10;
\t infoBox.value = x.toFixed(1);
}*/
function sqrt(x) {
x = infoBox.value;
x = Math.sqrt(x);
infoBox.value = x;
}
#move {
margin-top: 150px;
margin-left: auto;
margin-right: auto;
border: 5px solid grey;
border-radius: 10px;
background: grey;
}
body {
background-image: url("https://static.pexels.com/photos/27665/pexels-photo-27665.jpg");
background-size: 1600px 780px;
background-repeat: no-repeat;
}
h1 {
text-align: center;
}
#dimensions {
width: 165px;
}
.size2 {
width: 30px;
cursor: hand;
cursor: pointer;
}
<h1>FreecodeCamp Calculator Project</h1>
<table id="move">
<form>
<tr>
<th>
<input type="text" id="screen" disabled>
</th>
</tr>
<tr>
<td>
<input class="size2" type="button" value="MC">
<input class="size2" type="button" value="MR">
<input class="size2" type="button" value="MS">
<input class="size2" type="button" value="M+">
<input class="size2" type="button" value="M-">
</td>
</tr>
<td>
<input class="size2" type="button" value="←" onclick="backspace()">
<input class="size2" type="button" value="CE" onclick="backspace()">
<input class="size2" type="button" value="C" onclick="addscreen('C')">
<input class="size2" type="button" value="±" onclick="changesign('±')">
<input class="size2" type="button" value="√" onclick="sqrt()">
</td>
</tr>
<tr>
<td>
<input class="size2" type="button" value="7" onclick='addscreen("7")'>
<input class="size2" type="button" value="8" onclick="addscreen('8')">
<input class="size2" type="button" value="9" onclick="addscreen('9')">
<input class="size2" type="button" value="/" onclick="addscreen('/')">
<input class="size2" type="button" value="%" onclick="mutiplyBy2()">
</td>
</tr>
<tr>
<td>
<input class="size2" type="button" name="" value="4" onclick="addscreen('4')">
<input class="size2" type="button" name="" value="5" onclick="addscreen('5')">
<input class="size2" type="button" name="" value="6" onclick="addscreen('6')">
<input class="size2" type="button" name="" value="*" onclick="addscreen('*')">
<input class="size2" type="button" name="" value="x^2" onclick="mutiplyBy()">
</td>
</tr>
<tr>
<td>
<input class="size2" type="button" name="" value="1" onclick="addscreen('1')">
<input class="size2" type="button" name="" value="2" onclick="addscreen('2')">
<input class="size2" type="button" name="" value="3" onclick="addscreen('3')">
<input class="size2" type="button" name="" value="-" onclick="addscreen('-')">
<input class="size2" type="button" name="" value="=" onclick="product()">
</td>
</tr>
<tr>
<td>
<input class="size2" type="button" name="" value="0" onclick="addscreen('0')">
<input class="size2" type="button" name="" value="." onclick="addscreen('.')">
<input class="size2" type="button" name="" value="+" onclick="addscreen('+')">
</td>
</tr>
</form>
</table>
當你從輸入中得到一個值時,它是一個強大的。使用** parseInt **將其轉換爲整數。 – user2182349
parseInt不會這樣做,因爲您要從所有輸入中創建一個字符串。你需要做的是保持最後一種價值分離並改變它。這樣你可以追加它並稍後操作。順便說一句:爲了更好的可讀性,我會建議正確縮進你的代碼:) – lumio