2014-12-05 42 views
0

我最近编写了一段代码,可以在一段时间内检查某段课程的温度。我使用不同的代码编写程序,但找不到我遇到的一些问题。JSFiddle代码遇到的问题

我只是想知道如果任何人都可以看到,我错过任何明显的错误,可以帮忙带一些资料诠释他的正确方向。

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); 
+0

您需要从'getCity()'方法返回城市。 – Heslacher 2014-12-05 14:14:31

回答

1
function getCity() { 
    //get the locale to help with the display 
    city = prompt("Enter your city >> "); 
} 

//main program 
city = getCity(); 

你好像缺少一个return语句。

此外,该行total = total + next;似乎是格格不入:我想象中的totaltemperaturestotal,不0 + temperatureOfLastDay