I wrote a bank script that verifies if the user's deposit amount is a positive integer. If it's not, I want to reject the transaction.
Check out my code snippet below:
<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 attempted to address the issue with ..
else if(Number(depositAmt) < 0) {
return alert('please enter a valid integer.');
}
However, this method did not work as expected. Can anyone provide insight into what I might be doing incorrectly?
Thank you in advance!