jeudi 1 novembre 2018

How to keep the variable from returning to original value each time I use the onClick event

Hi I have the following HTML and javascript code for an ATM withdrawl machine. The code works but the balance does not decrease (starts at 400$) after each withdrawl from the user.I can't find what is the problem or how to get around this issue. Any help would be appreciated.

<!DOCTYPE html>
<html>

<head>
    <title>ATM</title>
    <style>
        #bank {
            text-align: center;
        }

        button {
            padding-left: 6px;
            padding-right: 6px;
        }
    </style>
    <script type="text/javascript" src="Q2.js">
    </script>
</head>

<body>
    <form id="bank">
        <h2> Question 2 </h2>
        <h5>ATM Machine</h5>
        <input type="text" id="cash" placeholder="Enter withdrawal amount" size="25">
        <button type="submit" onclick="validateAmount()" id="button">Submit</button>
    </form>
</body>

</html>

Here is the javascript

balance = 400.00;


function validateAmount() {
    a = document.getElementById("cash");
    amount = a.value;

    if (isNaN(amount)) {
        alert("Please enter a numeric value");
    } else {

            withdrawlAmount();
    }
}

function withdrawlAmount() {


    if (amount >= balance)
        alert("Insufficent Funds");
    else if (amount % 20 != 0)
        alert("Incorrect withdrawl amount");
    else if (amount < balance) {
        alert("Succeful transaction\nCurrent Balance: " + (balance - amount) + "$");
        balance -= amount;
    } else {
        return;
    }
}

Aucun commentaire:

Enregistrer un commentaire