P
proxygeek
I could not find any worthwhile discussion on implementing a generic
swap(a,b) function in Javascript.
Please point me to a discussion/link if it's already covered there.
Problem:
I need a method swap(a,b) which I can call with two variables as
params and it would swap the values of the two vars.
<snip>
....
....
function swap(a,b){ // This would NOT work as
intended
t = a;
a = b;
b = t;
}
....
....
var x = 10, y = 20;
swap(x,y);
alert("x = " + x + ", y = " + y); // should give x =
20, y = 10
....
</snip>
Above implementation would not work as here swap() does not know a
thing about x or y.
What would be the best solution for this?
Regards,
proxygeek
swap(a,b) function in Javascript.
Please point me to a discussion/link if it's already covered there.
Problem:
I need a method swap(a,b) which I can call with two variables as
params and it would swap the values of the two vars.
<snip>
....
....
function swap(a,b){ // This would NOT work as
intended
t = a;
a = b;
b = t;
}
....
....
var x = 10, y = 20;
swap(x,y);
alert("x = " + x + ", y = " + y); // should give x =
20, y = 10
....
</snip>
Above implementation would not work as here swap() does not know a
thing about x or y.
What would be the best solution for this?
Regards,
proxygeek