error-handling - JSFiddle代码遇到的问题

标签 error-handling

我最近写了一段代码,用于检查一段时间内的温度以进行某些类(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;行似乎不合适:我想象totaltotaltemperatures,而不是0 + temperatureOfLastDay

关于error-handling - JSFiddle代码遇到的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27317446/

相关文章:

asp.net - 良好的错误处理实践

r - 为什么na.rm在我的代码中不起作用?

error-handling - 如果Option为Some,返回Err的惯用方式

php - 使用 PDO 处理错误的最佳实践

c - 在程序内部使用 EXIT_SUCCESS/EXIT_FAILURE 进行错误报告可以吗?

C - 检查文件中的错误

python-3.x - 多处理 Pipe() 包装器损坏 : Something is Hanging. V5

PHP 得到警告和错误信息?

javascript - undefined variable 作为函数参数 javascript

c# - List<int> 中用户输入的错误处理