0

I have this problem to solve. It sounds maybe simple but I am a total beginner. here is my code so far but there is only one window that appears when I run it. Thank you for your advices and help

var years= prompt("Enter the number of years: "); // Number of years
var rainFall = new ArrayList();

while (years < 1) {

    alert("Invalid. Enter 1 or greater: ");
    years =prompt("Enter a year: ");
}

for (var y = 1; y <= years; y++) {

    for (var m = 1; m <= NUM_MONTHS; m++) {

        alert("Year " + y + " month " + m + ": ");
        monthRain = prompt("enter rainfall for a month ");
        rainfall.add(monthRain);
    }
}

 alert("\nNumber of months: " + (years * NUM_MONTHS) + "Total rainfall: " + calculateTotalRainFall(rainFall) + " inches" + "Average monthly rainfall: " + calculateAverageRainFall(rainFall) + " inches");
1
  • Hi, try changing rainfall.add(monthRain); to rainfall.push(monthRain); Commented Jul 20, 2015 at 12:28

3 Answers 3

1

    Array.prototype.sum = function() {
      var total = 0;
      for(var i in this) {
        var i = parseFloat(this[i]);
        if(i>=0) {
          total += i;
        }
      }
      return total;
    };

    Array.prototype.avg = function() {
      var avg = (this.length>0)? parseFloat(this.sum()/this.length) : 0;
      avg = parseFloat(parseInt(avg*100)/100);
      return avg;
    };

    var years = parseInt(prompt("Enter the number of years: ")); // Number of years

    var rainFall = [];

    while (years < 1) {
      alert("Invalid. Enter 1 or greater: ");
      years = parseInt(prompt("Enter a year: "));
    }

    var NUM_MONTHS = 12;
    for (var y = 1; y <= years; y++) {
      for (var m = 1; m <= NUM_MONTHS; m++) {
        alert("Year " + y + " month " + m + ": ");

        var monthRain = parseFloat(prompt("enter rainfall for a month "));
        if(monthRain>=0) {
          rainFall.push(monthRain);
        }
      }
    }

 if(years<1) years = 0;
 alert("\nNumber of months: " + (years * NUM_MONTHS) + "   Total rainfall: " + rainFall.sum() + " inches" + "   Average monthly rainfall: " + rainFall.avg() + " inches");

OR:

    function sumArray (items) {
      var total = 0;
      for(var i in items) {
        var i = parseFloat(items[i]);
        if(i>=0) {
          total += i;
        }
      }
      return total;
    };

    function avgArray(items) {
      var sum = sumArray(items);
      var avg = (items.length>0)? parseFloat(sum/items.length) : 0;
      avg = parseFloat(parseInt(avg*100)/100);
      return avg;
    };

    var years = parseInt(prompt("Enter the number of years: ")); // Number of years

    var rainFall = [];

    while (years < 1) {
      alert("Invalid. Enter 1 or greater: ");
      years = parseInt(prompt("Enter a year: "));
    }

    var NUM_MONTHS = 12;
    for (var y = 1; y <= years; y++) {
      for (var m = 1; m <= NUM_MONTHS; m++) {
        alert("Year " + y + " month " + m + ": ");

        var monthRain = parseFloat(prompt("enter rainfall for a month "));
        if(monthRain>=0) {
          rainFall.push(monthRain);
        }
      }
    }

 if(years<1) years = 0;
 alert("\nNumber of months: " + (years * NUM_MONTHS) + "   Total rainfall: " + sumArray(rainFall) + " inches" + "   Average monthly rainfall: " + avgArray(rainFall) + " inches");

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer but it is far from what I have learned in class now I am not supposed to use function yet. Just basic pseudocode
sorry I meant to not use function but I keep your answers for my next works. Thanks a lot for your time
0

try changing rainfall.add(monthRain); to rainfall.push(monthRain);

Comments

0

It looks like your core mistake was using the Java ArrayList class, when you should have been using the JavaScript Array class. Keep in mind that Java and JavaScript are not related (the name is confusing, yes).

If you change new ArrayList() to new Array(), and rainfall.add to rainFall.push (note the capitalization change as well), then it seems to work.

If you have access to a JavaScript console inspector (accessible in Chrome, for instance, with ctrl-shift-J), you can look for the JS errors that occur in your code, such as the error encountered when ArrayList couldn't be found.

Fixed below. I've also added the missing NUM_MONTHS constant (which I assume you had elsewhere).

var NUM_MONTHS = 12;
var years= prompt("Enter the number of years: "); // Number of years

// CHANGE: use "Array", not "ArrayList". You could also use "var rainFall = [];", which I find
var rainFall = new Array();

while (years < 1) {

    alert("Invalid. Enter 1 or greater: ");
    years =prompt("Enter a year: ");
}

for (var y = 1; y <= years; y++) {

    for (var m = 1; m <= NUM_MONTHS; m++) {

        alert("Year " + y + " month " + m + ": ");
        monthRain = prompt("enter rainfall for a month ");
        // CHANGE: use "push()" method of Array, and use camelCase for variable name consistency
        rainFall.push(monthRain);
    }
}

alert("\nNumber of months: " + (years * NUM_MONTHS) + "Total rainfall: " + calculateTotalRainFall(rainFall) + " inches" + "Average monthly rainfall: " + calculateAverageRainFall(rainFall) + " inches");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.