您的位置:首页 > Web前端 > JavaScript

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

2013-12-01 11:27 429 查看
好久没来图书馆了,今天本来打算专注学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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: