Hi, need some help with javascript. I have to search my array for the lowest tem
ID: 3550231 • Letter: H
Question
Hi, need some help with javascript. I have to search my array for the lowest temperature and display the lowest temp with the corresponding date. The code for searching the lowest temp that I have so far is at the end. What I have is obviously wrong as it displays :
"The lowest temp ofInfinityoccured on-1"
Any tips on what I did wrong with that section would be great.
// Anonymous function for temperature data
// this function is automatically called when the form is submitted
(function(){
//Array to hold temp data
var temperatures = new Array();
// function which updates the array
function addTemp() {
var lowTemp = document.getElementById("lowTemp").value;
var highTemp = document.getElementById("highTemp").value;
if(lowTemp.length === 0 || highTemp.length === 0)
{
alert("Valid low and high temperatures must be entered!");
}
else
{
document.getElementById("output").style.display="block";
// Add new temperatures to the array
// Current date
var d = new Date();
if(temperatures.length == 0)
{
// Current date if it is 1st temperature
temperatures.push(new Array(d.getMonth(), d.getDate(), d.getFullYear(), lowTemp, highTemp));
}
else
{
// 1 day before last date
newDate = temperatures[temperatures.length - 1][1] - 1;
temperatures.push(new Array(d.getMonth(), newDate, d.getFullYear(), lowTemp, highTemp));
}
// Create the output table
var tempTable = document.getElementById("tempTable");
tempTable.innerHTML = "<tr><th>Date</th><th>Low Temperatures</th><th>High Temperatures</th></tr>";
// Loop over the array and create the table
// Also calculate the averages
var avgLow = 0;
var avgHigh = 0;
temperatures.forEach(function(entry)
{
var newRow = document.createElement('tr');
newRow.innerHTML = '<td>' + entry[0] + '/' + entry[1] + '/' + entry[2] + '</td><td align="right">' + entry[3] + '</td><td align="right">' + entry[4] + '</td>';
tempTable.appendChild(newRow);
avgLow += parseInt(entry[3]);
avgHigh += parseInt(entry[4]);
});
// Add the row for average
avgLow /= temperatures.length;
avgHigh /= temperatures.length;
avgLow = avgLow.toFixed(1)
avgHigh = avgHigh.toFixed(1)
var newRow = document.createElement('tr');
newRow.innerHTML = '<td>Averages</td><td align="right">' + avgLow + '</td><td align="right">' + avgHigh + '</td>';
tempTable.appendChild(newRow);
//Add row for lowest temp
var minTemp = Math.min.apply(lowTemp.value);
var key = lowTemp.indexOf(d);
var newRow = document.createElement('tr');
newRow.innerHTML = '<td colspan="3"> The lowest temp of' + minTemp + 'occured on' + key + '</td>';
tempTable.appendChild(newRow);
;
}
return false;
}
function init() {
'use strict';
document.getElementById('theForm').onsubmit = addTemp;
} // End of init() function.
window.onload = init;
})();
Explanation / Answer
import java.io.*; class CityTemp { publicstaticvoid main(String args[]) { String str; double temp[][]=newdouble[11][32]; double HighestCityTemp[]=newdouble[11]; double LowestCityTemp[]=newdouble[11]; HighestCityTemp[0]=0.0; LowestCityTemp[0]=0.0; try{ BufferedReader obj = new BufferedReader(new InputStreamReader(System.in)); System.out.println(" =====Finding TOP 10 cities higest and lowest temperature===== "); for(int i=1;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.