遇到JSFiddle代码的问题

时间:2014-12-05 13:54:32

标签: error-handling

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

我只是想知道是否有人可以看到我遗漏的任何明显的错误,并且可以在正确的方向帮助提供一些信息。

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

1 个答案:

答案 0 :(得分:1)

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

//main program
city = getCity();

看起来你错过了一个返回声明。

此外,行total = total + next;似乎不合适:我认为totaltotal的{​​{1}},而不是temperatures