Inverse array function with one parameter. (JavaScript)

I am having a problem trying to write a body for a function that recursively reverses an array, but only has one parameter.

function ReverseArray(arr) {

  var i = 0;
  var j = arr.length - 1;

  if (i < j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return ReverseArray(arr);
  }

  return arr;
}   

      

I understand that this won't work because the variables will be reinitialized when the function is called.

I am just looking for some ideas at this stage because I am stuck.

+3


source to share


9 replies


First, for other people who might be looking for a reverse array for a real problem and not for homework, use Array.reverse , don't waste time trying to implement it yourself.

If you absolutely must, then a smaller array will be passed here (minus the first and last elements using slice ) when you overwrite and restore the final array when you relax using concat . For example:



function ReverseArray(arr) {
  if (arr.length < 2) {
    return arr;
  } else {
    var first = arr[0];
    var last = arr[arr.length - 1];
    return [last].concat(ReverseArray(arr.slice(1, length - 1))).concat([first]);
  }

}

alert(ReverseArray([1,2,3,4,5]));
      

Run codeHide result


+2


source


There is an Array.reverse method .

Anyway ... for testing / training:

["a","b","c"].map(function(v,i,a) {return a[a.length-i-1]})

      

v is the value, i is the iteration, and a is the array.

1st iteration:



v = "a"; i = 0; a = ["a", "b", "c"];

Second iteration:

v = "B"; i = 1; a = ["a", "b", "c"];

3rd iteration:

v = "C"; i = 2; a = ["a", "b", "c"];

+1


source


Why would you want to write your own reverse method? Why not just use the reverse method on the array?

function ReverseArray(arr) {
    arr = arr.reverse();
}

      

UPDATE

The above method obviously doesn't make a lot of sense as you can just call the inverse method wherever you want.

0


source


I think the following solution is the simplest recursive implementation:

var a = [1,2,3,4,5];
alert(ReverseArray(a));

function ReverseArray(arr) {
    if(arr.length < 2) {
        return arr;
    } else {
        return [arr.pop()].concat(ReverseArray(arr));
    }
}
      

Run codeHide result


0


source


I use shift, pop, unshift and push to avoid partial copies of the array. It's probably a bit faster than copying a portion of the array using a slice on every recursive call, but don't expect it to outperform a non-recursive solution or built-in reverse method:

function recReverse(list){
  if(list.length>=2){
    var lo=list.shift();
    var hi=list.pop();
    list=recReverse(list);
    list.unshift(hi);
    list.push(lo);
  }
  return list;
}

      

and fiddle

0


source


Let's say you have an array [1 2 3 4 5]

Just store the last number somewhere and feed 1-4 to the function again (resulting in 5-1234)

When fed only one number, return that number as one element of the array.

When the array comes back, add the returned array to the temp number in the new array.

It should be 4-123, then 3-12, then 2-1, which will be fully loaded, resulting in 5-4-3-2-1

Compressed version:

var input_array = [1,2,3,4,5]

function rev(input) {
  if (input.length == 1) { return input; }
  if (input.length == 0) { return []; }
  return [input[input.length-1]].concat(rev(input.splice(0, input.length-1)));
}

rev(input_array);

      

Expand version:

var input_array = [1,2,3,4,5]

function rev(input) {
  if (input.length == 1) { return input; }
  if (input.length == 0) { return []; }
  var last = [input[input.length-1]];
  var newarr = rev(input.splice(0, input.length-1));
  var answer = last.concat(newarr);
  return answer;
}

rev(input_array);

      

-1


source


what you need to do is

function reverse(x, i, j) {
    if (i < j) {//Swap
        var tmp = x[i];
        x[i] = x[j];
        x[j] = tmp;
        reverse(x, ++i, --j);//Recursive
    }
}

function reverseIt(x) {
    console.log(x);
    reverse(x, 0, x.length - 1);
    console.log(x);
}


var q = [1, 2, 3, 4, 5, 6, 7, 8, 9];
reverseIt(q);

      

-1


source


function reverseArray(origArray)
{
    if (origArray.length > 1) {
        var element = origArray[0];
        var newArray = reverseArray(origArray.slice(1));
        newArray[newArray.length] = element;
        return newArray;
    } else {
        return origArray;
    } 
}

      

-1


source


var result = [];
function reverseArray(arr) {
  result.push(arr.pop());
  j= arr.length;
  if (j>0) {
   reverseArray(arr);   
  }   
}  
reverseArray([1,4,3,5]);
console.log(result);

      

-1


source







All Articles