jeudi 20 février 2020

Variable numbers not updating in my if else statement

I'm trying to run this simple banking "app", but I can't get the values to update properly. I've tried prompting userDesposit by pressing "d" as my first input and then prompted userBalance by pressing "b", but the value would always be alerted as 0 no matter what number I enter. Also on my first run through, I can't get it to quit after prompting some choices - but I can get it to quit right away if I were to press "q" as my first choice. Can someone help me fix these? Thanks!

function userBank() { 
  let userBalance = 0;
  let continueBanking = true; 
  let userInput = prompt ("Enter 'q' to quit immediately, Enter 'w' to withdraw money, Enter 'd' to deposit money, Enter 'b' to view your balance.");
  
  while (continueBanking == true) {
    if (userInput == "w") {
      let userWithdraw = prompt ("How much would you like to withdraw?");
      userBalance = userWithdraw;
      userBank();
    } else if (userInput == "d") {
          let userDeposit = prompt("How much would you like to deposit?");
          userBalance = userDeposit;
      userBank();
    } else if (userInput == "b") {
      alert("Here is your current balance: " + userBalance);
      userBank();
    } else if (userInput == "q") {
      alert("Banking app is now closing.");
      continueBanking == false;
      return;
    }
    else {
      alert("Invalid user input. Try again.");
      return;
    }
  }
}

userBank();

Aucun commentaire:

Enregistrer un commentaire