Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Can't subtract Ints after converting with ParseInt

Tags:

javascript

The variable logincompare_user is taken from login that gives us the position where the user is in a list and their corresponding money will be in that same position in the ahorros and monetaria lists. Why this subtract isn't working?

var ahorros = [5000,6000,10000];
var monetaria = [2000,15000,1000];
var registro = [];

function transferencias(ahorros ,monetaria,registro,logincompare_user){
    var cta_ahorro = ahorros[logincompare_user];
    var cta_monetaria = monetaria[logincompare_user];
    registro.push("Entrada a Transferencias " + Math.floor(Date.now()/1000) + '\n');
    var eleccion_cta = prompt('Bienvenid@ a Transferencias, que desea hacer?\n'+
                              "1 - Ahorros a Monetaria\n"+
                              '2 - Monetaria a Ahorros\n'+
                              '0 - Log Out');
    while (eleccion_cta >= 0 && eleccion_cta <= 2){
        if (eleccion_cta == 1){
            var transfer = prompt("Cuanto desea transferir?",0);
            var trans_int = parseInt(transfer,0);
            if (trans_int <= cta_ahorro){
                cta_ahorro = cta_ahorro - transfer;
                cta_monetaria = cta_monetaria + transfer;
            }
            else{
                alert("Error");
                transferencias(ahorros,monetaria,registro,logincompare_user);
            }
        }
        else if (eleccion_cta == 2){
            var transfer_1 = prompt("Cuanto desea transferir?");
            if (transfer_1 <= cta_monetaria && transfer_1 === parseInt(transfer_1)){
                cta_ahorro = cta_ahorro + transfer_1;
                cta_monetaria = cta_monetaria - transfer_1;
            }
            else{
                alert("Ingrese un numero por favor");
                transferencias(ahorros,monetaria,registro,logincompare_user);
            }
        }
        else if (eleccion_cta == 0){
            log_out(logincompare_user,registro,ahorros,monetaria)
        }
        else{
            alert("Opcion Invalida");
            transferencias(ahorros,monetaria, registro,logincompare_user)
        }
    }
like image 769
Rodrigo Cano Avatar asked Nov 10 '22 00:11

Rodrigo Cano


1 Answers

You are adding/subtracting transfer variable to cta_ahorro and cta_monetaria instead of trans_int variable. transfer is a string, when you try to add it with an int variable it is causing problem. I have corrected your code and mentioned below:

var ahorros = [5000,6000,10000];
var monetaria = [2000,15000,1000];
var registro = [];

    function transferencias(ahorros, monetaria, registro, logincompare_user) {
    var cta_ahorro = ahorros[logincompare_user];
    var cta_monetaria = monetaria[logincompare_user];
    console.log(cta_ahorro); // Printing for confirmation 
    console.log(cta_monetaria); // Printing for confirmation 
    registro.push("Entrada a Transferencias " + Math.floor(Date.now() / 1000) + '\n');
    var eleccion_cta = prompt('Bienvenid@ a Transferencias, que desea hacer?\n' +
            "1 - Ahorros a Monetaria\n" +
            '2 - Monetaria a Ahorros\n' +
            '0 - Log Out');

    while (eleccion_cta >= 0 && eleccion_cta <= 2) {
      if (eleccion_cta == 1) {
        var transfer = prompt("Cuanto desea transferir?", 0);
        var trans_int = parseInt(transfer, 0);
        if (trans_int <= cta_ahorro) {
          cta_ahorro = cta_ahorro - trans_int; // Here is change 
          cta_monetaria = cta_monetaria + trans_int; // Here is change 
          console.log(cta_ahorro); // Printing for confirmation 
          console.log(cta_monetaria); // Printing for confirmation 
        }
        else {
          alert("Error");
          transferencias(ahorros, monetaria, registro, logincompare_user);
        }
      }
      else if (eleccion_cta == 2) {
        var transfer = prompt("Cuanto desea transferir?");
        var trans_int = parseInt(transfer, 0);
        if (trans_int <= cta_monetaria) {
          cta_ahorro = cta_ahorro + trans_int; // Here is change 
          cta_monetaria = cta_monetaria - trans_int; // Here is change 
          console.log(cta_ahorro); // Printing for confirmation 
          console.log(cta_monetaria); // Printing for confirmation 
        }
        else {
          alert("Ingrese un numero por favor");
          transferencias(ahorros, monetaria, registro, logincompare_user);
        }
      }
      else if (eleccion_cta == 0) {
        log_out(ahorros, monetaria, registro, logincompare_user)
      }
      else {
        alert("Opcion Invalida");
        transferencias(ahorros, monetaria, registro, logincompare_user)
      }
      var eleccion_cta = prompt('Bienvenid@ a Transferencias, que desea hacer?\n' +
            "1 - Ahorros a Monetaria\n" +
            '2 - Monetaria a Ahorros\n' +
            '0 - Log Out');
    }
  }
like image 122
Furqan Aziz Avatar answered Nov 14 '22 23:11

Furqan Aziz