This is fun. Okay, I'm not a JavaScript programmer, but here are my submissions (the first two solutions taking 2-3 minutes to create):
First we create the array.
var ary = [1,2,3,4,5];
Now... First reversal method, assuming static array size and these particular numbers:
ary = [5,4,3,2,1];
Whether this is "in place" depends on how JavaScript works. Seems likely to me that it'll do it in place.
Second method, assuming static array size, this particular array size, and using any (numeric!) values within the array:
for (i = 0; i < 2; i++)
{
t = ary[i];
ary[i] = ary[5-i-1];
ary[5-i-1] = t;
}
Obviously uses a temporary variable. Depending on what was meant, that may violate the "in place".
Okay, after a couple more minutes, here's another:
for (i = 0; i < 2; i++)
{
ary[i] += ary[5-i-1];
ary[5-i-1] = ary[i] - ary[5-i-1];
ary[i] -= ary[5-i-1];
}
Again, assuming a 5 element array. No memory aside from the array used, though more (simple arithmetic) operations used.
Oh, shit, hey... Does this work?
for (i = 0; i < 2; i++)
{
ary[i] ^= ary[5-i-1];
ary[5-i-1] ^= ary[i];
ary[i] ^= ary[5-i-1];
}
Seems to. I'm not that knowledgeable, so I don't know this, but I get the sense that binary manipulations may be faster than arithmetic.
Also, I expect things like the 5-i-1 to get evaluated once and for the compiler to not bother calculating it again. If this were a concern, then storing the result in a temporary variable might make sense, depending on the overhead (and whether this again violates "in place").
Critiques, please?