Wasabi
Wasabi

Reputation: 1591

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(''); 
return str;

Would someone enlighten my usage?

Upvotes: 0

Views: 85

Answers (2)

gilly3
gilly3

Reputation: 91497

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;

Upvotes: 3

FishBasketGordo
FishBasketGordo

Reputation: 23142

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.

Upvotes: 1

Related Questions