最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: function returning NAN - Stack Overflow

programmeradmin7浏览0评论

I'm working on a codecademy lesson with instructions to write the calculateTotal function below. When I click run, it's returning NaN. Anyone know what's wrong with the calculateTotal function as I wrote it that's making it return NaN. Note, I understand that NaN means not a number...

// 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];

// declare your function calculateTotal here
var calculateTotal = function(raceTimes){
    var totalTime; 
    for(i = 0; i < raceTimes.length; i++){
        totalTime += raceTimes[i]; 
        return totalTime; 
    }
};

var liuTotal = calculateTotal(liu);

console.log(liuTotal);

Note, many of the people answering this question have said that var totalTime has to be set to "O". However, in the next codecademy lessson, the author writes a function with totalTime not set to anything and it works

var calculateAverage = function (raceTimes) {
  var totalTime;
  for ( i = 0; i < raceTimes.length; i++ ) {
    totalTime = (totalTime || 0) + raceTimes[i];
  }
// assign variable averageTime
var averageTime = totalTime / raceTimes.length; 

  return averageTime;
};

I'm working on a codecademy. lesson with instructions to write the calculateTotal function below. When I click run, it's returning NaN. Anyone know what's wrong with the calculateTotal function as I wrote it that's making it return NaN. Note, I understand that NaN means not a number...

// 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];

// declare your function calculateTotal here
var calculateTotal = function(raceTimes){
    var totalTime; 
    for(i = 0; i < raceTimes.length; i++){
        totalTime += raceTimes[i]; 
        return totalTime; 
    }
};

var liuTotal = calculateTotal(liu);

console.log(liuTotal);

Note, many of the people answering this question have said that var totalTime has to be set to "O". However, in the next codecademy lessson, the author writes a function with totalTime not set to anything and it works

var calculateAverage = function (raceTimes) {
  var totalTime;
  for ( i = 0; i < raceTimes.length; i++ ) {
    totalTime = (totalTime || 0) + raceTimes[i];
  }
// assign variable averageTime
var averageTime = totalTime / raceTimes.length; 

  return averageTime;
};
Share Improve this question edited Aug 27, 2012 at 21:51 Leahcim asked Aug 27, 2012 at 21:37 LeahcimLeahcim 42k61 gold badges202 silver badges343 bronze badges 4
  • 3 Well at least it's not a function returning NyaN – Pointy Commented Aug 27, 2012 at 21:38
  • 2 a) You're not setting totaltime to zero (or another value) to initialise it. b) You're returning after only one iteration – Lee Taylor Commented Aug 27, 2012 at 21:39
  • You don't have to set totalTime to zero. But set it to any number you want, so then you can add a number to a number and get a number. Otherwise it is not a number. – Ivan Kuckir Commented Aug 27, 2012 at 21:41
  • @IvanKuckir That's what I said – Lee Taylor Commented Aug 27, 2012 at 21:44
Add a ment  | 

3 Answers 3

Reset to default 8

Two problems:

  1. totalTime is not defined -- adding something to an undefined results in NaN
  2. You are returning INSIDE your loop.

Fix:

var totalTime=0;
for(i = 0; i < raceTimes.length; i++){
    totalTime += raceTimes[i]; 
}
return totalTime; 

You haven't initialized totalTime with a value. So it defaults to undefined. Therefore on each iteration, undefined is being added, yielding NaN.

Your totalTime doesn't have an initial value, so it starts off as undefined. You can't add a number to undefined and get a number:

var totalTime; // implicitly initialized to undefined 
for(i = 0; i < raceTimes.length; i++){
    totalTime += raceTimes[i];  // undefined + raceTimes[i] = NaN
    return totalTime; 
}

Initialize it to 0.

var totalTime = 0;
发布评论

评论列表(0)

  1. 暂无评论