Reputation: 625
I have a bank script. When the user deposits money I want to make sure it is a positive integer. If it isn't, I want to kick them out.
Here is my code:
<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;
}
I tried using ..
else if(Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}
But that doesn't work. What am I doing wrong?
Thanks guys!
Upvotes: 0
Views: 4223
Reputation: 341
Check like this:
if(isNaN(Number(depositAmt)) || Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}
Upvotes: 1
Reputation: 1993
Just check
if (depositAmt <= 0) {
return alert('Please enter a positive integer.');
}
Rewrite your if
loop as if
- else if
- else
to avoid checking all conditions.
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-->
Upvotes: 1