I'm new to javascript and i just can't seem to get this right. When I get to the deposit function of my atm, I'm supposed to test an invalid input and get a error message. I do get that but efterwards I just get thrown out. Works fine with a correct input.
The same thing happen when I come to my withdrawal function. In this part I can't even insert e correct value. I get an error message and get thrown out again.
let balance = 100;*/
let accountName = "Missy Jones",
balance = 100;
function getBalance() {`enter code here`
alert('Your current balance is: '+ balance);
atm();
}
function deposit() {
let deposit = parseFloat(prompt('How much would you like to deposit?'));
if (isNaN(deposit) || deposit === ''|| deposit === 0 || deposit < 0) {
alert('Error: please enter a valid input!');
deposit();
} else {
balance += deposit;
getBalance();
atm();
}
}
function withdrawal() {
let withdrawal = parseInt(prompt("How much do you want to withdraw?"));
if (isNaN(withdrawal) || withdrawal === "" || withdrawal === 0 || withdrawal > 0) {
alert("Input invalid, please try again");
withdrawal();
atm();
} else {
balance -= withdrawal;
getBalance();
atm();
}
}
function getAccountName () {
alert("Name of account: " +accountName)
atm();
}
function error() {
alert('Error: accepted numbers are 1 through 4.');
atm();
}
function exit() {
let myWindow =
function closeWin() {
myWindow.close();
};
}
function atm() {
var choice = parseInt(prompt('Select a choice 1.) Balance 2.) Deposit 3.) Withdrawal 4.) Get Account Name 5.) Exit'));
if (choice === 1) {
getBalance();
} else if (choice === 2) {
deposit();
} else if (choice === 3) {
withdrawal();
} else if (choice === 4) {
getAccountName();
} else if (choice === 5) {
exit();
} else {
error();
}
}
atm();
In the deposit function, you created a variable called deposit. During a bad input you call the function deposit again, but at this point this is a local variable so, you get an error. Same goes for withdrawal. By changing the value variable to a different name, your code works
let accountName = "Missy Jones",
balance = 100;
function getBalance() {`enter code here`
alert('Your current balance is: '+ balance);
atm();
}
function deposit() {
let depositValue = parseFloat(prompt('How much would you like to deposit?'));
if (isNaN(depositValue) || depositValue === ''|| depositValue === 0 || depositValue < 0) {
alert('Error: please enter a valid input!');
deposit();
} else {
balance += depositValue;
getBalance();
atm();
}
}
function withdrawal() {
let withdrawalValue = parseInt(prompt("How much do you want to withdraw?"));
if (isNaN(withdrawalValue) || withdrawalValue === "" || withdrawalValue === 0 || withdrawalValue > 0) {
alert("Input invalid, please try again");
withdrawal();
atm();
} else {
balance -= withdrawalValue;
getBalance();
atm();
}
}
function getAccountName () {
alert("Name of account: " +accountName)
atm();
}
function error() {
alert('Error: accepted numbers are 1 through 4.');
atm();
}
function exit() {
let myWindow =
function closeWin() {
myWindow.close();
};
}
function atm() {
var choice = parseInt(prompt('Select a choice 1.) Balance 2.) Deposit 3.) Withdrawal 4.) Get Account Name 5.) Exit'));
if (choice === 1) {
getBalance();
} else if (choice === 2) {
deposit();
} else if (choice === 3) {
withdrawal();
} else if (choice === 4) {
getAccountName();
} else if (choice === 5) {
exit();
} else {
error();
}
}
atm();