Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(){    // <3
  2.     'use strict';
  3.  
  4.     // This script averages Low and High daily temperatures as input by the user.
  5.     // Global variables:
  6.     var lowTemps = [];
  7.     var highTemps = [];
  8.     var dates = [];
  9.  
  10.     var lowSum = 0;
  11.     var highSum = 0;
  12.     var highest = 0;
  13.     var lowest = 0;
  14.  
  15.     var highestMsg = '';
  16.     var lowestMsg = '';
  17.  
  18.     // Function called when the form is submitted.
  19.     // Function adds high and low temperatures to the global array.
  20.     function addTemps() {
  21.         // These get the input elements
  22.         var highTemp = document.getElementById('highTemp').value;
  23.         var lowTemp = document.getElementById('lowTemp').value;
  24.        
  25.         var output = document.getElementById('output');
  26.  
  27.          // For the output:
  28.         var message = '';
  29.         var error;
  30.        
  31.         if (isNaN(highTemp) || isNaN(lowTemp) || (highTemp < lowTemp)) {
  32.             // ensures that both variables are NUMBERS
  33.             error = "Please enter valid temperatures.";
  34.         } else {
  35.             // Add a new date!
  36.             var today = new Date();
  37.             today.setDate(today.getDate() - dates.length);
  38.             dates.push(today);
  39.            
  40.             // Add the temps to the array only if they validate:
  41.             highTemp = parseFloat(highTemp);
  42.             lowTemp = parseFloat(lowTemp);
  43.            
  44.             if (highTemps.length == 0 || lowTemps.length == 0) {
  45.                 highest = highTemp;
  46.                 lowest = lowTemp;
  47.                 highestMsg = 'The highest temperature of ' + highest + ' occured on ' + formatDate(today);
  48.                 lowestMsg = 'The lowest temperature of ' + lowest + ' occured on ' + formatDate(today);
  49.             }
  50.            
  51.             if (highTemp > highest) {
  52.                 highest = highTemp;
  53.                 highestMsg = 'The highest temperature of ' + highest + ' occured on ' + formatDate(today);
  54.             }
  55.             if (lowTemp < lowest) {
  56.                 lowest = lowTemp;
  57.                 lowestMsg = 'The lowest temperature of ' + lowest + ' occured on ' + formatDate(today);
  58.             }
  59.            
  60.             highSum += highTemp;
  61.             lowSum += lowTemp;
  62.            
  63.             highTemps.push(highTemp);
  64.             lowTemps.push(lowTemp);
  65.            
  66.             // error still undefined because there was no error :)
  67.         }
  68.        
  69.         // Update the page:
  70.         message = '<table id="table"><th>Date</th><th>Low Temperatures</th><th>High Temperatures</th>';
  71.  
  72.         for (var i = 0; i < highTemps.length; i++) {
  73.             var dateStr = formatDate(dates[i]);
  74.             message += '<tr>' + '<td class="alt2">' + dateStr + '</td>' + '<td class="right">' + lowTemps[i] + '</td>' + '<td class="right">' + highTemps[i] + '</td>' + '</tr>';
  75.         }
  76.  
  77.         // Now calculate averages and dump in the table
  78.         message += '<tr>';
  79.         if (error) {
  80.             message += '<td colspan="3">' + error + '</td>';
  81.         } else {
  82.             message += '<td>Average:</td>' + '<td class="right">' + (lowSum / lowTemps.length).toFixed(1) + '</td>' + '<td class="right">' + (highSum / highTemps.length).toFixed(1) + '</td>';
  83.         }
  84.         message += '</tr>';
  85.        
  86.         // Display highest high and lowest low
  87.         message += '<tr><td colspan="3">' + lowestMsg + '</td></tr>';
  88.         message += '<tr><td colspan="3">' + highestMsg + '</td></tr>';
  89.        
  90.         // End table
  91.         message += '</table>';
  92.  
  93.         // Display the table    
  94.         output.innerHTML = message;
  95.  
  96.         return false;
  97.     }
  98.    
  99.     function formatDate(date) {
  100.         return date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
  101.     }
  102.  
  103.     function init() {
  104.         'use strict';
  105.    
  106.         document.getElementById('temps').onsubmit = addTemps;
  107.     }
  108.  
  109.     window.onload = init;
  110.  
  111. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement