Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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.");
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.");
  }
}


over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!