日期:2014-05-16  浏览次数:20427 次

为什么不是数字的相加而是字符串的联接?
var   sumCount=[ "12 ", "12 ", "13 "];//sumCount是以字符串的方式取得的数字数组//
var   s=0;
  for(i=0;i <sumCount.length;i++){
  s=(s-0)+(sumCount[i]-0);
    }

javascript中这类问题应怎么处理?

------解决方案--------------------
<script language= "javaScript ">
var sumCount=[ "12 ", "12 ", "13 "];//sumCount是以字符串的方式取得的数字数组//
var s=0;
for(i=0;i <sumCount.length;i++){
s=(s-0)+(eval(sumCount[i])-0);
}

</script>
------解决方案--------------------
<script language= "javascript ">
var sumCount=[ "12 ", "12 ", "13 "];//sumCount是以字符串的方式取得的数字数组//
var s=0;
for(i=0;i <sumCount.length;i++){
s+=parseInt(sumCount[i]);
}
alert(s);
</script>

------解决方案--------------------
1+1 === 1-(-1)
:D

<script language= "javascript ">
var sumCount=[ "12 ", "12 ", "13 "];//sumCount是以字符串的方式取得的数字数组//
var s=0;
for(i=0;i <sumCount.length;i++){
s =-(-s)-(-sumCount[i]);
}
alert(s);
</script>
------解决方案--------------------
<script type= "text/javascript ">
var sumCount = "1,1.4 ".split( ", ");
var s = 0;
for (i = 0 ; i < sumCount.length ; i ++) {
s += Number(sumCount[i]);
}
alert(s);
</script>
晕倒,还以为你要转串呢。