Search code examples
javascriptswitch-statement

Keep total from resetting on basic banking app


I'm working on a basic banking app for a beginning JS class.

Expected:

  • user clicks button that calls banking app function with a switch statement to choose to withdraw, deposit, check balance or quit
  • after choosing withdraw or deposit, they're prompted to add the amount
  • log in the console the amount of deposit/withdrawal and the the new balance
  • every subsequent button click and withdrawal/deposit should keep track of the balance

Actual:

  • The subtotal is resetting because of the switch statement, I can't figure out how to keep the running total without deleting the break.

Is there a way to adjust this code to keep a running balance?

function bankingApp() {
  let currentBalance = 0;
  let userPrompt = prompt(
    "bank menu: w = withdrawal | d = deposit | b = balance |  q = quit"
  );

  switch (userPrompt) {
    case "w":
      function withdrawFunds() {
        let withdrawAmount = parseFloat(prompt("Withdraw amount: "));
        currentBalance = currentBalance - withdrawAmount;
        console.log(
          "Withdraw: " + withdrawAmount + "New balance: " + currentBalance
        );
      }
      withdrawFunds();
      break;

    case "d":
      function depositFunds() {
        let depositAmount = parseFloat(prompt("Deposit amount:"));
        console.log(
          "Deposit: " + depositAmount + "New balance: " + currentBalance
        );
      }
      depositFunds();
      break;

    case "b":
      function checkBalance() {
        let balance = currentBalance;
        console.log(balance);
      }
      checkBalance();
      break;

    case "q":
      function quitProgram() {
        let quit = "Quit the program.";
        console.log(quit);
      }
      quitProgram();
      break;

    default:
      console.log("That menu is not available.");
  }
}

Solution

  • You are calling your function on an event happening(button click). Now the first statement in your function is setting the balance to 0. There is nothing keeping track of it.

    Simply move the statement out, so the variable persists.

    let currentBalance = 0;
    
    function bankingApp() {
    
      let userPrompt = prompt(
        "bank menu: w = withdrawal | d = deposit | b = balance |  q = quit"
      );
    
      switch (userPrompt) {
        case "w":
          function withdrawFunds() {
            let withdrawAmount = parseFloat(prompt("Withdraw amount: "));
            currentBalance = currentBalance - withdrawAmount;
            console.log(
              "Withdraw: " + withdrawAmount + "New balance: " + currentBalance
            );
          }
          withdrawFunds();
          break;
    
        case "d":
          function depositFunds() {
            let depositAmount = parseFloat(prompt("Deposit amount:"));
            console.log(
              "Deposit: " + depositAmount + "New balance: " + currentBalance
            );
          }
          depositFunds();
          break;
    
        case "b":
          function checkBalance() {
            let balance = currentBalance;
            console.log(balance);
          }
          checkBalance();
          break;
    
        case "q":
          function quitProgram() {
            let quit = "Quit the program.";
            console.log(quit);
          }
          quitProgram();
          break;
    
        default:
          console.log("That menu is not available.");
      }
    }