0

I want to use the same variable array to output the reverse an integer array. But, I only got the first value print out. Not sure where I did wrong. Here is my code:

let array = [3, 8, 9, 6, 4, 2]
reverseArray(array);
function reverseArray(array){
    let left =0;
    for(i =  0; i <= array.length - 1; i++){
        let right = array.length - 1 - i;
        let temp = right;
        right = left;
        left = temp;
        array = array[left];
    }
    console.log(array);
}
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42

5 Answers5

2

Assuming this is a learning exercise, note that you are iterating too many times:

function reverseArray(array){
    const len = array.length;

    for (let i = 0; i < len/2; ++i) {
        const temp = array[i];
        const tail = len - i - 1;
        array[i] = array[tail];
        array[tail] = temp;
    }
}

const array = [3, 8, 9, 6, 4, 2];
reverseArray(array);
console.log(array);

As others have pointed out, the reason for truncation is the fact that you assigned an array element to the array variable, turning it from an array into a scalar.

"In real life", you should use array.reverse().

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

it's because you set the array as a single value by doing array = array[left].

you just override the array variable and placing a single element inside it.

changing this line to array[right] = array[left] would be a good start.

if you want to fix it all, you need to iterate only through HALF of the array (so that you won't reverse it twice - back to normal), and make the whole swap (not just replace one element):

let array = [3, 8, 9, 6, 4, 2]
reverseArray(array);
function reverseArray(array){
    for(i =  0; i < array.length / 2; i++){
        let right = array.length - 1 - i;
        let temp = array[right];
        array[right] = array[i];
        array[i] = temp;
    }
    console.log(array);
}

Note: if you want to reverse an array you can simply use the array prototype "reverse" function like this:

let array = [3, 8, 9, 6, 4, 2];
array = array.reverse();

console.log(array);
Liad
  • 789
  • 4
  • 10
  • `array[right] = array[left]` this is not valid result!!! just run and check your result – Alireza Ahmadi Aug 19 '21 at 19:18
  • i know that, just said that it would be a nice place to start, the problem now is that he only swaps one element (he should also save the `right` element and place in the `left` index – Liad Aug 19 '21 at 19:30
0

Just use build in NodeJs function:

array.reverse()
spatak
  • 1,039
  • 1
  • 14
  • 25
0

What you are actually doing wrong is

array = array[left]

On the right side of this equation is a value of array at a specific index while on the right side of this array is a whole array

0

Actually you changing index so right use to store index not value and in the last line array = array[left] is you are trying to give value and that is array[0] = 1 so that's why 1 is coming Right way to do this

let array = [3, 8, 9, 6, 4,2]
reverseArray(array);

function reverseArray(array){
for(i =  0; i <= (array.length - 1)/2; i++){// need to iterate only half otherwise it remain same so for exampale 0th index replace with 4th index and 4th with 0 so at last both at their same position
    let right = array.length - 1 - i;
    let temp = array[right];
    array[right] = array[i];
    array[i] = temp;

}
console.log(array);}