Example Program
  1. // example.js
  2. // Input: An integer, listLen, where listLen is less
  3. // than 100, followed by listLen-numeric values
  4. // Output: The number of input values that are greater
  5. // than the average of all input values
  6.  
  7. var intList = new Array(99);
  8. var listLen, counter, sum = 0, result = 0;
  9.  
  10. listLen = prompt (
  11. "Please type the length of the input list", "");
  12.  
  13. if ((listLen > 0) && (listLen < 100)) {
  14. // Get the input and compute its sum
  15. for (counter = 0; counter < listLen; counter++) {
  16. intList[counter] = prompt (
  17. "Please type the next number", "");
  18. sum += parseInt(intList[counter]);
  19. }
  20.  
  21. // Compute the average
  22. average = sum / listLen;
  23.  
  24. // Count the input values that are > average
  25. for (counter = 0; counter < listLen; counter++) {
  26. if (intList[counter] > average) result++;
  27. // Display the results
  28. document.write("Number of values > average is: ",
  29. result, "<br />");
  30. }
  31.  
  32. } else
  33. document.write("Error - input list length is not legal <br />");