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]
10
  • Set a breakpoint and look at what's happening to the values in the array. Commented Dec 18, 2018 at 14:57
  • console.log() and breakpoints are your friends Commented Dec 18, 2018 at 15:00
  • 2
    You are not returning anything from the function. If you return arr at the end, you'll see it works. Commented Dec 18, 2018 at 15:01
  • 1
    Possible duplicate of javascript reverse an array without using reverse() Commented Dec 18, 2018 at 15:02
  • 2
    @LeviBerkowitz there is not a "right way" . What does "right way" mean? Commented Dec 18, 2018 at 15:07

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This works by accident as arr is dropped. Remove arr = and it will still work.
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.

4 Comments

@quirimmo nope, variables are passed by value.
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.
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
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
0

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

1 Comment

Not sure why that was downvoted. This is accurate, and explains the reason.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.