每天进步一点点---------JS之平均时间计算

来源:互联网 发布:windows桌面壁纸故事 编辑:程序博客网 时间:2024/04/25 21:14

好久没来图书馆了,今天本来打算专注学python,但是还是想着回顾了一下js,我始终相信学习就像骑自行车一样,学会了就是你的宝藏

今天的代码是计算田径比赛的平均成绩,也可以套用在很多求平均值的地方,需要注意的是不能用+=来求总数,原因我还没想出来......反正结果是错的,谨记


// Runner times
var carlos = [9.6,10.6,11.2,10.3,11.5];
var liu = [10.6,11.2,9.4,12.3,10.1];
var timothy = [12.2,11.8,12.5,10.9,11.1];


// Define the function calculateAverage
var calculateAverage = function(raceTime){
    var totalTime;
    for ( var i in raceTime){
        totalTime = (totalTime||0)+raceTime[i];
    }
    var averageTime = totalTime/raceTime.length;
    return averageTime;
};
var isQualified = function (runner) {
  // Assign the variable averageTime
    var averageTime = calculateAverage(runner);
  if ( averageTime >= 11.5 ) { 
    // Times greater than or equal to 11.5 are too slow
console.log("Close, but you didn't make the cut.");
  } else if ( averageTime < 11.5 ) {
// An average time of less than 11.5 can join the team
console.log("Welcome to the team, speedy!");
  }
};


// Call the function isQualified on liu and timothy
isQualified(liu);
isQualified(timothy);