...

View Full Version : concat stings



nikko50
10-12-2008, 07:40 PM
How can I append the value x to x during the below loop? Can I do something like below?




for (i=0;i<n;i++){

x =. x ;

}

abduraooft
10-12-2008, 07:42 PM
Use + instead.

Trinithis
10-12-2008, 10:06 PM
x += "thing to append"

Kor
10-13-2008, 03:33 PM
for (var i=0;i<n;i++){
x+=x;
}

rnd me
10-13-2008, 03:43 PM
x = x + x

is usually faster than +=

Kor
10-13-2008, 07:49 PM
If we are speaking about the speed, then a while loop is faster than a for loop, and a pre-increment is faster than a post-increment


var i=0;
while(++i<n){
x=x+x;
}

Trinithis
10-13-2008, 09:19 PM
x = x + x

is usually faster than +=

At the cost of higher maintenance?

If you really want speed, I believe this is the fastest . . . using Array.prototype.join:


// Fill an array of size n with all elements set to x.
// For simplicity, I'll cheat in making it. Use a loop/duff's device if you really care.

x = Array.replicate(n, x).join("");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum