Reverse array function with one parameter. (JavaScript)

杀马特。学长 韩版系。学妹 提交于 2019-12-23 18:03:58

问题


I'm having a problem trying to write a body for a function that recursively reverse 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 realize this won't work because the variables will be re-initialized when the function calls itself.

I'm just looking for some ideas at this point, because i'm stuck.


回答1:


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

If you absolutely must, then the trick here would be to pass a smaller array (minus the first and last elements by using slice) when you recurse and rebuild the final array as you unwind 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]));



回答2:


There is an Array.reverse method.

Anyways... for testing/learning purposes:

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

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

1st iteration:

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

2nd iteration:

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

3rd iteration:

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




回答3:


Why do 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 does not make a lot of sense anymore since you can just call the reverse method wherever you want.




回答4:


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));
    }
}



回答5:


I'm using shift, pop, unshift, and push to avoid making partial copies of the array. Probably a tad bit faster than copying of part of the array using slice on each recursive call, but don't expect it to outperform a non-recursive solution or the 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




回答6:


what you need to do is this

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);



回答7:


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);



回答8:


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;
    } 
}



回答9:


Say you have an array [1 2 3 4 5]

Simply store the last number somewhere, and feed 1-4 into the function again (resulting in 5-1234)

When there is only one number in the fed in array, return that number as a single array member.

When the array is returned back, append the returned array to the temporary number into a new array.

This should do 4-123, then 3-12, then 2-1, which will all feed back up, resulting in 5-4-3-2-1

Condensed 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);

Spread out 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);


来源:https://stackoverflow.com/questions/26836808/reverse-array-function-with-one-parameter-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!