ATM simulator

3

I am starting to put together a program that simulates an ATM with 3 banking options and an option for the user to terminate the process. But after the user-made operation, I tried to use the multiple decision command switch case within the loop while to return to the do start menu, but the method did not work:

  var opcao, vl_saq;
  var saldo = 0;
  var min = 0;
  var max = 2000;
  do {
    opcao = parseInt(prompt("Escolha uma opção:\n1 - Saque\n2 - Depósito\n3 - Saldo\n0 - Sair")); // MENU inicial
  }
  while (opcao != 0){
    switch (opcao) {
      case 1:
        vl_saq = parseInt(prompt("Digite o valor do saque:"));
        if (vl_saq > saldo) {
          if (vl_saq > 0 && vl_saq < max) {
            alert("O saque está sendo realizado...\nAperte em OK");
            saldo -= vl_saq;
            alert("Operação Realizada!");
          }
          else {
            alert("Só podem entrar no saque, valores que sejam entre R$0,00 e R$2.000,00");
          }
        }
        else{
          alert("Saldo insuficiente! Você pode sacar\nR$ "+saldo.toFixed(2));
        }
      break;
    }
  }

When running the code, script runs normally in the browser, but only that it always remains in the start menu, not running the rest of the program.

In this way, which method would be most appropriate to solve this case?

    
asked by anonymous 15.12.2018 / 20:25

1 answer

7

There are other problems, but the main problem is where while is. The do block needs to encapsulate every execution to exit only when 0 is typed. It actually amazes me that this syntax was accepted because while is ending do and at the same time starting another block then. p>

One of the reasons for creating confusion is not to worry about the most appropriate syntax. In general, languages accept certain forms of code writing and give the impression of being something that it really is not. The correct syntax, and consequently the correct semantics would look like this:

var opcao, vl_saq;
var saldo = 0;
var min = 0;
var max = 2000;
do {
    opcao = parseInt(prompt("Escolha uma opção:\n1 - Saque\n2 - Depósito\n3 - Saldo\n0 - Sair")); // MENU inicial
    switch (opcao) {
    case 1:
        vl_saq = parseInt(prompt("Digite o valor do saque:"));
        if (vl_saq > saldo) {
            if (vl_saq > 0 && vl_saq < max) {
                alert("O saque está sendo realizado...\nAperte em OK");
                saldo -= vl_saq;
                alert("Operação Realizada!");
            } else alert("Só podem entrar no saque, valores que sejam entre R$0,00 e R$2.000,00");
        } else alert("Saldo insuficiente! Você pode sacar\nR$ "+saldo.toFixed(2));
        break;
    }
} while (opcao != 0);
    
15.12.2018 / 20:38