我最近写了一段代码,用于检查一段时间内的温度以进行某些类(class)学习。我使用不同的代码位编写了程序,但是找不到遇到的某些问题。
我只是想知道是否有人可以看到我所缺少的任何明显错误,并且可以在正确的方向上提供一些信息。
Link to the JSFiddle
//Counting the average temperatures in a day
//needs debugged!!!
var temperatures = [];
var total = 0;
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
function getNumDays() {
number = prompt("How many days in the study? Enter 1 - 10");
while ((number < 1) || (number > 10) ||( isNaN(number) === true)) {
alert ("Invalid input!");
number = prompt ("Enter again, 1 - 10 >> ");}
return number;
}
function getTemps(numDays) {
total = 0;
for (i = 0; i < numDays; i++) {
next = prompt ("Enter the temperature for day " + (i+1));
next = parseint(next);
while (isNaN(next)===true) {
next = 0;
next = prompt ("Error in input! Try again >>");
next = parseInt(next);}
temperatures.push(next);
}
total = total + next;
return temperatures;
}
function calcAverage(total, numDays) {
average = total / numDays;
return average;
}
function showStatistics(city, average, numdays) {
alert ("The average daily temperature for "+ city + " is " + average.toFixed(2) + " measured over " + numDays + " days." );
}
//main program
city = getCity();
numDays = getNumDays();
temperatures = getTemps(numDays);
Average = calcAverage(total, numDays);
showStatistics(city, average, numDays);
最佳答案
function getCity() {
//get the locale to help with the display
city = prompt("Enter your city >> ");
}
//main program
city = getCity();
好像您缺少return语句。
另外,
total = total + next;
行似乎不合适:我想象total
是total
的temperatures
,而不是0 + temperatureOfLastDay
。
关于error-handling - JSFiddle代码遇到的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317446/