我有一个银行脚本。当用户存钱时,我想确保它是一个正整数。如果不是,我想把它们踢出去。
这是我的代码:
<section id="pigBox">
<img src="images/pig.png" />
<label>Balance: </label><input type="text" id="balance" />
<button id="deposit"> Deposit </button>
<button id="withdraw"> Withdraw </button>
</section><!-- end of pigBox-->
document.getElementById('balance').value = "1000"
var balance = document.getElementById('balance').value;
var deposit = document.getElementById('deposit');
var withdraw = document.getElementById('withdraw');
deposit.addEventListener('click', depositCash);
withdraw.addEventListener('click', withdrawCash);
function depositCash() {
var depositAmt = prompt('How much would you like to deposit?');
if(depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
function withdrawCash() {
var withdrawAmt = prompt('How much you you like to withdraw?');
if(withdrawAmt != Number(withdrawAmt)) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) - Number(withdrawAmt);
document.getElementById('balance').value = balance;
}
我尝试过使用..
else if(Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}
但这不起作用。我做错了什么?
谢谢你们!
答案 0 :(得分:1)
请检查
if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
}
将if
循环重写为if
- else if
- else
,以避免检查所有条件。
if (depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
} else if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
} else {
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
document.getElementById('balance').value = "1000"
var balance, deposit, withdraw;
balance = document.getElementById('balance').value;
deposit = document.getElementById('deposit');
withdraw = document.getElementById('withdraw');
deposit.addEventListener('click', depositCash);
withdraw.addEventListener('click', withdrawCash);
function depositCash() {
var depositAmt;
depositAmt = prompt('How much would you like to deposit?');
if (depositAmt != Number(depositAmt) && depositAmt) {
return alert('Please enter a valid integer.');
} else if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
} else {
balance = Number(balance) + Number(depositAmt);
document.getElementById('balance').value = balance;
}
}
function withdrawCash() {
var withdrawAmt;
withdrawAmt = prompt('How much you you like to withdraw?');
if (withdrawAmt != Number(withdrawAmt)) {
return alert('Please enter a valid integer.');
}
balance = Number(balance) - Number(withdrawAmt);
document.getElementById('balance').value = balance;
}
&#13;
<section id="pigBox">
<img src="images/pig.png" />
<label>Balance: </label><input type="text" id="balance" />
<button id="deposit"> Deposit </button>
<button id="withdraw"> Withdraw </button>
</section>
<!-- end of pigBox-->
&#13;
答案 1 :(得分:1)
像这样检查:
if(isNaN(Number(depositAmt)) || Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}