0

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?

2
  • 2
    Where is piece declared/defined? Commented Aug 30, 2011 at 17:33
  • What's the problem? What error do you get? What is str set to when this is done? Commented Aug 30, 2011 at 17:35

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Was my interpretation of the following example in err? aymanh.com/…
What is piece and where does it come from? The example shown demonstrates building up a string into an Array and then joining it. But, it doesn't show where the elements of the array should come from - that part is left to you and your specific needs. If you already have an array of strings, there is no need for a for loop, you can just call .join() on your array.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.