2017-04-12 9 views
0

私には銀行のスクリプトがあります。ユーザーがお金を入金するとき、私はそれが正の整数であることを確認したい。もしそうでなければ、私はそれらを追い出したい。負の数をチェックする文の場合

は、ここに私のコードです:

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

しかし、それは動作しません。私は間違って何をしていますか?

ありがとうございました!

+1

'しかし、それはwork'しない、それが何を意味するのでしょうか?間違いはありますか? – gurvinder372

+0

http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer –

+0

[文字列が正の整数であることを検証する]の可能な複製(http://stackoverflow.com)/questions/10834796/validate-that-a-string-is-a-integer-integer) –

答えて

1

だけ

if (depositAmt <= 0) { 
    return alert('Please enter a positive integer.'); 
    } 
ifとしてあなた ifループを書き換え

チェック - すべての条件を確認し避けるためにelseからelse if。このような

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

1

チェック:

if(isNaN(Number(depositAmt)) || Number(depositAmt) < 0) { 
    return alert('please enter a valid integer.'); 
} 
関連する問題