Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
I can't get the correct value on on my deposit or withdrawal function. What is wrong?

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();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda