Struggling with a JavaScript challenge - I need help creating a JavaScript script where the user inputs the month and the amount of rain for that month, and the program generates a table showing the total rain for each repeated month. For example, if January is entered three times, the table should display the sum of rainfall for those three entries.
I've attempted to write the code but it keeps looping twice for repeated months and I can't figure out how to stop it after calculating the total rainfall for the repeats. Here's the current code:
var proceed = true;
function Month(month, rain) {
this.month = month;
this.rain = rain;
}
data = [];
months = ["January", "February"];
while (proceed) {
var month = prompt("Enter the month", "");
var rain = parseInt(prompt("Enter the millimeters of rain for the month", ""));
new Month(month, rain);
data.push(new Month(month, rain));
proceed = confirm("Do you want to continue?");
}
for (var j = 0; j < data.length; j++) {
if (data[j].month == months[0]) {
var initialValue = 0;
var sum = data.reduce(function(accumulator, currentValue) {
return accumulator + currentValue.rain;
}, initialValue)
document.write("<table>");
document.write("<tr>");
document.write("<td>" + data[j].month + "</td>");
document.write("<td>" + sum + "</td>");
document.write("</tr>");
document.write("</table>");
} else {
document.write("<table>");
document.write("<tr>");
document.write("<td>" + data[j].month + "</td>");
document.write("<td>" + data[j].rain + "</td>");
document.write("</tr>");
document.write("</table>");
}
}