Use of accumulators in matrix element and iteration counter in Do..While

0

Hi, I'm learning Javascript and I've come across this:

 var depositos=[];
  var nro,monto;
  do {
    nro=prompt('Ingrese nro de cliente','');

    nro=parseInt(nro)
    if (nro!=0)
    {
      monto=prompt('Ingrese monto a depositar','');

      monto=parseInt(monto);

      depositos[nro]=monto;
    }
  } while (nro!=0);
  var suma=0;
  var canti=0;
  for(var f=0;f<depositos.length;f++)
  {
    if (depositos[f]!==undefined)
    {
      suma=suma+depositos[f];
      canti++;
    }
  }
  document.write('Cantidad de depósitos:'+canti+'<br>');
  document.write('Total depositado por todos los clientes:'+suma);

Basically what it does is request a customer value that will be used as the index of the array and an amount to deposit that will be the value of that index, to show them with document.write the total of deposits and the total of the amounts deposited, the program ends by entering a 0 in the client number. My problem is that I do not know how to do the following:

1) That if, when entering the same client number for the same time the same index of the array, instead of replacing the old index value of the array with the new one, the old value is added with the new value , because if for the second time I enter the same client number (index of the array), and enter a new value different from the previous one, this index value is replaced by the new one, what should I do?

2) The same with the amount of deposits, it happens to me that if in the first and second time I put the same index (client number), I only count 1 deposit independently that the values are different.

    
asked by Eduardo Sebastian 26.04.2017 в 18:55
source

3 answers

1
  • In depositos[nro] = monto; change the = by += , but first we must ensure that the element exists.
  • By putting canti++; in the for that traverses the elements of the array, you are counting the elements. To count the deposits, this should be included in do .
  • var depositos=[];
    var nro,monto,canti=0;
    
    do {
      nro=prompt('Ingrese nro de cliente (0 para salir)','');
    
      nro=parseInt(nro)
      if (nro!=0)
      {
        monto=prompt('Ingrese monto a depositar','');
    
        monto=parseInt(monto);
        // Línea nueva. Si elemento de la matriz no existe, inicializarlo con 0
        (depositos[nro] == undefined) ? depositos[nro] = 0: false;
        // Línea modificada. Cambiar = por +=
        depositos[nro] += monto;
        // Contador de depósitos
        canti++
      }
    } while (nro!=0);
    
    var suma=0;
    //var canti=0;
    
    for(var f=0;f<depositos.length;f++)
    {
      if (depositos[f]!==undefined)
      {
        suma=suma+depositos[f];
        //canti++;
      }
    }
    document.write('Cantidad de depósitos:'+canti+'<br>');
    document.write('Total depositado por todos los clientes:'+suma);
        
    answered by 26.04.2017 / 19:21
    source
    0

    The problem that you have here is more than the whole algorithm.

    First, what do you want to get to? what results are you looking for?

    Notice that point 1 is solved by changing

    depositos[nro]=monto;
    

    for

    depositos[nro]=depositos[nro]+monto;
    

    and the second point is a problem, because with your current data structure, you really do not know how many real deposits there are. What you can do is count the amount of deposits entered by the user at the beginning, each time he makes an income.

    I am still assuming that this is more of a theoretical exercise than real, because I would never use the client number as an index of a vector.

        
    answered by 26.04.2017 в 19:04
    0

    I can think of this, save the number of the client and the amount in an associative array, even if the client is the same, it does not matter and you can get the amount, you could even know how many times the client deposited x.

    Example

        var depositos=[];
      var nro,monto;
      do {
        nro=prompt('Ingrese nro de cliente','');
    
        nro=parseInt(nro)
        if (nro!=0)
        {
          monto=prompt('Ingrese monto a depositar','');
    
          monto=parseInt(monto);
    
          depositos.push({
            cliente: nro,
            monto : monto
          });
        }
      } while (nro!=0);
      var suma=0;
      var canti=0;
      for(var f=0;f<depositos.length;f++)
      {
        if (depositos[f]!==undefined)
        {
          suma=suma+depositos[f].monto;
          canti++;
        }
      }
      document.write('Cantidad de depósitos:'+canti+'<br>');
      document.write('Total depositado por todos los clientes:'+suma);
    
        
    answered by 26.04.2017 в 20:39