gpt4 book ai didi

javascript - 检查负数的 If 语句

转载 作者:行者123 更新时间:2023-12-01 03:49:08 24 4
gpt4 key购买 nike

我有一个银行脚本。当用户存钱时我想确保它是一个正整数。如果不是的话,我想把他们踢出去。

这是我的代码:

<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.');
}

但这行不通。我做错了什么?

谢谢大家!

最佳答案

只需检查

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;
}
<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-->

关于javascript - 检查负数的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43362216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com