`
wzy1986714
  • 浏览: 11588 次
  • 性别: Icon_minigender_1
  • 来自: 温州
最近访客 更多访客>>
社区版块
存档分类
最新评论

A problem caused by careless use of variable type: TC code

 
阅读更多
   var value=new Array(1024,768,250,480,540,800,600,933,700);
   for(var i in value)
   {
     Log.Message((1+i)<5?(i):(i+1))
   }

 The result for this program:

01  11  21  31  41  51  61  71  81

 

seems weird, what I expexted is : 0 1 2 3 5 6 7 8 9

 

After debug found root cause is that:

for(var i in value)
 

here var i is treated as string later, NOT as intenger!

 

Correct Code should be:

  var value=new Array(1024,768,250,480,540,800,600,933,700);
   for(var i in value)
   {
     i=parseInt(i);
     Log.Message((1+i)<5?(i):(i+1))
   }
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics