0

I'm self-studying data structures and am clearly not grasping how referencing works.

I'm trying to reverse an array WITHOUT creating a new array. So I think I need to store one of the values in a local variable (x in this case) but even so, it's not updating.

function reverseArrayInPlace(arr){
    for (var i = 0; i < Math.floor(arr.length / 2); i++){
        let x = i;
        arr[arr.length - 1 - i] = arr[i];
        arr[i] = arr[x];
    }
    return arr;
}

let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);

My expected result should just be any array reversed.

// → [5, 4, 3, 2, 1]

But I'm just getting

// → [1, 2, 3, 4, 5]

GrafiCode
  • 3,307
  • 3
  • 26
  • 31

4 Answers4

2

You should store the value you're going to be replacing in x like so:

let x = arr[arr.length - 1 - i];

...then replace it (ie: do arr[arr.length - 1 - i] = arr[i]), and then use x (the value you just replaced) to set the value of arr[i] to "swap" the two values

See example below:

function reverseArrayInPlace(arr){
    for (var i = 0; i < Math.floor(arr.length / 2); i++){
        let x = arr[arr.length - 1 - i];
        arr[arr.length - 1 - i] = arr[i];
        arr[i] = x;
    }
    return arr;
}

let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

Since you are using let, I'm assuming you are using ES2015 features. You can use destructuring to swap the array items.

function reverseArrayInPlace(arr){
    for (var i = 0; i < Math.floor(arr.length / 2); i++){
        [arr[arr.length - 1 - i], arr[i]] = [arr[i], arr[arr.length - 1 - i]]
    }
    return arr;
}

let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
adiga
  • 34,372
  • 9
  • 61
  • 83
0

You could try using:

function reverseArrayInPlace(arr){
    for (var i = 0; i < Math.floor(arr.length / 2); i++){
        let temp = arr[i];
        arr[i] = arr[arr.length - i - 1];
        arr[arr.length - i - 1] = temp;
    }
    return arr;
}
Ralf
  • 16,086
  • 4
  • 44
  • 68
0

You have few problems.

First of all, you need to store the value in x, not the index. If you are swapping values then one of the two values will be overriden, that's why you need a copy of it.

Second, the line arr[i] = arr[x] is incorrect - arr[x] with x = i is literally just arr[i] = arr[i]. Also, as discussed previously, the value will be overwritten, hence you want so save a value and assign that to a new location, otherwise you will link to the overwritten value and just duplicate it in both places.

function reverseArrayInPlace(arr){
    for (var i = 0; i < Math.floor(arr.length / 2); i++){
        //arr[arr.length - 1 - i] will be overwritten, so saving that value as x
        let x = arr[arr.length - 1 - i];
        //overwrite the the value
        arr[arr.length - 1 - i] = arr[i];
        //swap the old value to a new position
        arr[i] = x;
    }
    return arr;
}

let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
VLAZ
  • 26,331
  • 9
  • 49
  • 67