A question was asked to me today and I do not believe it is possible, but I could be wrong or am over thinking it. How can you reverse an array without using iteration in C?
Here's a neat solution using recursion in a javascript function. It does not require any parameters other than the array itself.
/* Use recursion to reverse an array */
function reverse(a){
if(a.length==undefined || a.length<2){
return a;
}
b=[];
b.push(reverse(copyChop(a)));
b.push(a[0]);
return b;
/* Return a copy of an array minus the first element */
function copyChop(a){
b=a.slice(1);
return b;
}
}
Call it as follows;
reverse([1,2,3,4]);
Note that if you don't use the nested function copyChop to do the array slicing you end up with an array as the first element in your final result. Not quite sure why this should be so