Soy nuevo en javascript y parece que no puedo hacerlo bien. Cuando llego a la función de depósito de mi cajero automático, se supone que debo probar una entrada no válida y recibir un mensaje de error. Lo entiendo, pero después me echan. Funciona bien con una entrada correcta.
Lo mismo sucede cuando llego a mi función de retiro. En esta parte ni siquiera puedo insertar el valor correcto. Recibo un mensaje de error y me expulsan de nuevo.
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();En la función de deposit , creó una variable llamada deposit . Durante una entrada incorrecta, vuelve a llamar a la función deposit , pero en este punto esta es una variable local, por lo que obtiene un error. Lo mismo ocurre con la withdrawal . Al cambiar la variable de valor a un nombre diferente, su código funciona
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();