1

I am trying to write a function which reverses the elements of an array without defining a new empty array in the function.

let arrayValue = [1, 2, 3, 4, 5]

function remove(array, index) {
return array.slice(0, index).concat(array.slice(index + 1));
}

function reverseArrayInPlace(array) {
  for (i = array.length - 2; i >= 0; i--) {
    let removedCharacter = array[i];
    array = array.concat(removedCharacter);
    array = remove(array, i);
  } 
    return array;
}

When I console.log(reverseArrayInPlace(arrayValue)) I am getting the reverse order of [5, 4, 3, 2, 1].

However when I try to just do reverseArrayInPlace(arrayValue) and then console.log(arrayValue), I am getting [1, 2, 3, 4, 5] which is the value defined at the beginning.

Is there a way of updating the arrayValue binding in the function and then when it is console.log outside the function, it is showing the reversed order?

2
  • 1
    Your remove function isn't in-place at all. An in-place solution would not reassign the array variable, and it would not need to return a value. An in-place solution would actually mutate the passed-in array by swapping its elements with each other. Please try that and show us your attempt. Think non-functional. Commented Mar 3, 2019 at 22:10
  • The solution for this exercise had the return statement at the end so I was assuming that my function had to return something as well. Thank you for the clarification on in-place. Commented Mar 4, 2019 at 21:21

4 Answers 4

0
// show cases of even and odd lengths
const x = [1,2,3,4];
const y = [1,2,3,4,5];

for (let i = 0; i < x.length / 2; i++) {
  const tmp = x[i];

  x[i] = x[x.length - 1 - i];
  x[x.length - 1 - i] = tmp;
}

for (let i = 0; i < y.length / 2; i++) {
  const tmp = y[i];

  y[i] = y[y.length - 1 - i];
  y[y.length - 1 - i] = tmp;
}

console.log(x);
// [4, 3, 2, 1]

console.log(y);
// [5, 4, 3, 2, 1]
Sign up to request clarification or add additional context in comments.

Comments

0

The MDN docs for Array's slice and concat methods explain that these methods return new arrays, rather than modifying the existing arrays. If you are looking for a built-in Array method for modifying arrays, splice will do the job. However, it's going to be more complicated to implement this using splice than to just use a for loop as the other answers suggest.

Comments

0

You could just swap values symmetrically around the midpoint of the array, like

const arr = [0,1,2,3,4];
const len = arr.length;
for(let i = 0; i < len/2; i++){
  let temp = arr[i];
  arr[i] = arr[len-1-i];
  arr[len-1-i] = temp;
}
console.log(arr);

1 Comment

This was the answer that I gave 30 minutes ago
-1
function reverseArrayInPlace(array) {
 for (let i = 0; i < array.length / 2; i++) {
  const oppositeArrayIndex = array.length - (i + 1);
  const oppasiteArrayValue = array[oppositeArrayIndex];
  array[oppositeArrayIndex] = array[i];
  array[i] = oppasiteArrayValue;
 }
}

2 Comments

I disagree, this code is more self-documenting thanks to variable naming than the one from the other answer, that you didn't mark as "low quality"
its just suggestion. generally code only answers will go to low quality queues by SO(its not me who will mark as low or high quality). it came to my review thats why suggested. hope you understood now.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.