0

A beginner JS question.. I need to write a function that reverses an array that goes as a function's input. (I cannot use a reverse method).

I wonder why this works:

function reverseArrayInPlace(array) {
  for (let i = 0; i < Math.floor(array.length / 2); i++) {
    let old = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;
  }
  return array;
}
let arr = [0, 1, 2, 3, 4, 5];
console.log(reverseArrayInPlace(arr))

But this does NOT:

function reverseArrayInPlace(arr) {
  let len = arr.length;
  for (counter = 0; counter < 2 * len; counter += 2) {
    arr.unshift(arr[counter]);
  }
  arr = arr.slice(0, len);
}
let b = [0, 1, 2, 3, 4, 5];
console.log(reverseArrayInPlace(b));

Looks like arr = arr.slice(0,len); part is not working..I wonder why when:

b = b.slice(0,6);
[5, 4, 3, 2, 1, 0]
chazsolo
  • 7,873
  • 1
  • 20
  • 44
Dr_V
  • 25
  • 5
  • Set a breakpoint and look at what's happening to the values in the array. – Hanlet Escaño Dec 18 '18 at 14:57
  • console.log() and breakpoints are your friends – sbattou Dec 18 '18 at 15:00
  • 2
    You are not returning anything from the function. If you `return arr` at the end, you'll see it works. – VLAZ Dec 18 '18 at 15:01
  • 1
    Possible duplicate of [javascript reverse an array without using reverse()](https://stackoverflow.com/questions/40751207/javascript-reverse-an-array-without-using-reverse) – Heretic Monkey Dec 18 '18 at 15:02
  • Actually, what @quirimmo says is correct. I missed that it has to do the switch in-place - you don't need to return anything in that case, you should modify the array passed in. – VLAZ Dec 18 '18 at 15:04
  • if you don't want to return anything and change the input array, use `splice` instead of `slice` like this: `arr = arr.splice(len);` and it should work – quirimmo Dec 18 '18 at 15:04
  • @vlaz I would go for not changing the input variable too, and return the new reversed array, but actually even the same `reverse` method in JS changes the input array. so maybe we are wrong :D – quirimmo Dec 18 '18 at 15:06
  • Here the right way to [reversing-an-array-without-reverse-or-duplicating-an-array](https://stackoverflow.com/a/45989220/6537245) – Levi Berkowitz Dec 18 '18 at 15:06
  • 2
    @LeviBerkowitz there is not a "right way" . What does "right way" mean? – quirimmo Dec 18 '18 at 15:07
  • @quirimmo I derive the requirement here by the function name `reverseArrayInPlace` - OP's explanation before that doesn't mandate a specific way to do it. The first function does modify in-place but then also returns the value, while the second one makes a copy but doesn't return anything. Considering the question, they should at least be consistent and true to their name. But yeah, I also don't like that JS executes `.reverse` in-place. It's annoying as it's counter to almost every other array method. – VLAZ Dec 18 '18 at 15:11

3 Answers3

2

If you want to change the input arrray, avoiding return, use splice:

function reverseArrayInPlace(arr) {
  let len = arr.length;
  for (counter = 0; counter < 2 * len; counter += 2) {
    arr.unshift(arr[counter]);
  }
  arr.splice(len);
}
var b = [0, 1, 2, 3, 4, 5];
reverseArrayInPlace(b);
console.log(b);

EDIT:

If you want to do something like:

console.log(reverseArrayInPlace(b));

your function MUST return something, otherwise the print will always be undefined, even if b has been reverted

quirimmo
  • 9,800
  • 3
  • 30
  • 45
1
   arr = arr.slice(0, len);

slice returns a new array which you store inside the local variable arr, that does not change b, which is still pointing to the whole array. To mutate the array you could:

 arr.splice(len, len);

and then you have to return arr to log something meaningful.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • @quirimmo nope, variables are passed by value. – Jonas Wilms Dec 18 '18 at 15:09
  • 1
    Well, arrays are objects passed by value...where the value is their reference. So, doing in-place modification of `arr` will also change it outside the function but `arr = /* something */` would not affect the array outside the function. – VLAZ Dec 18 '18 at 15:13
  • yeah that's what I meant, sorry bad reading. If he wants to change the input arr, avoiding returns, with splice he can directly modify it – quirimmo Dec 18 '18 at 15:18
  • then of course if he wants to do `console.log(reverseArrayInPlace(b));` the method must return it, otherwise the value printed will always be `undefined` because the function does not return anything – quirimmo Dec 18 '18 at 15:19
0

Because the array is passed to the function by copying the reference, hence you cannot change the external reference from inside the function.