开发者

Using Array Join for String Concatenation

Attempting using Array.join for string concatenation, but the following is causing FF to choke:

var tmp = ["disco","dance"];
for (i = 0; i<tmp.length; i++) {
  tmp.push(piece);
  alert(tmp[i]);
}
str = tmp.join(''); 
retur开发者_开发问答n str;

Would someone enlighten my usage?


You've got an infinite loop. Every iteration increases the length of tmp, so i will never be greater than tmp.length. Maybe this is what you want:

var tmp = ["disco","dance"];
var len = tmp.length;
for (i = 0; i < len; i++) {
  tmp.push(piece);
  alert(tmp[i]);
}
str = tmp.join(''); 
return str;

Edit: Or if piece doesn't really mean anything, just skip the for loop altogether:

var tmp = ["disco","dance"];
str = tmp.join(''); 
return str;


I'm not sure what you're trying to do with the loop. This, however, works:

var tmp = ["disco","dance"];
var str = tmp.join(''); 
return str; // Returns "discodance"

...which is just your original code without the loop. I suspect any trouble that you're having has to do with that loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜