javascript compare object prototype and sum them

I need help, I'm stuck with a javascript exercise, I need to create a script in javascript that the user insert the month and the ML of rain belong to that month and the program will returns a table with the month and the rain, but it needs to sum the rain of the repeat month, example January repeat three times so the table needs to show the sum of those three months.

I did this, but keep looping twice the repeat month and I don't know how break it after the sum of the repeat month, here is the code:

var proceed = true;

function Mes(mes, lluvia) {
  this.month = mes;
  this.rain = lluvia;
}

datos = [];

meses = ["Enero", "Febrero"];

while (proceed) {
  var mes = prompt("Ingresa el mes", "");
  var lluvia = parseInt(prompt("Ingrese los ml de lluvia correspondiente al mes", ""));

  new Mes(mes, lluvia);
  datos.push(new Mes(mes, lluvia));
  proceed = confirm("Deseas continuar?");
}


for (var j = 0; j < datos.length; j++) {
  if (datos[j].month == meses[0]) {
    var initialValue = 0;
    var sum = datos.reduce(function(accumulator, currentValue) {
      return accumulator + currentValue.rain;
    }, initialValue)
    //document.write("El acumulado de todos los meses ingresados es de " + sum);

    document.write("<table>");
    document.write("<tr>");
    document.write("<td>" + datos[j].month + "</td>");
    document.write("<td>" + sum + "</td>");
    document.write("</tr>");
    document.write("</table>");
  } else {
    document.write("<table>");
    document.write("<tr>");
    document.write("<td>" + datos[j].month + "</td>");
    document.write("<td>" + datos[j].rain + "</td>");
    document.write("</tr>");
    document.write("</table>");
  }
}

Kind regards,

1 answer

  • answered 2018-11-08 00:02 Barmar

    Create an object whose keys are the month names and values are the rain totals. Loop over your array and add each rain amount to the appropriate total.

    Also, you should only create one table; the loop should create just the rows.

    var proceed = true;
    
    function Mes(mes, lluvia) {
      this.month = mes;
      this.rain = lluvia;
    }
    
    datos = [];
    
    meses = ["Enero", "Febrero"];
    
    while (proceed) {
      var mes = prompt("Ingresa el mes", "");
      if (meses.includes(mes)) {
        var lluvia = parseInt(prompt("Ingrese los ml de lluvia correspondiente al mes", ""));
        datos.push(new Mes(mes, lluvia));
      } else {
        alert("Mal mes");
      }
      proceed = confirm("Deseas continuar?");
    }
    
    var totals = datos.reduce((obj, {month, rain}) => {
      if (month in obj) {
        obj[month] += rain;
      } else {
        obj[month] = rain;
      }
      return obj;
    }, {});
    
    document.write("<table>");
    document.write("<tr><th>Mes</th><th>Total</th></tr>");
    
    meses.forEach(month => {
      document.write("<tr>");
      document.write("<td>" + month + "</td>");
      document.write("<td>" + totals[month] + "</td>");
      document.write("</tr>");
    });
    document.write("</table>");