1

I recently started learning Javascript from Eloquent Javascript and got up until the way of data structures.

There is one Coding Exercise as Follows

write two functions, reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method.

Here is My Code

/*
 *  Create a Function reverseArray which returns a new array with the reversed array
 *  Create another Function reverseArrayInPlace() which modifies the ORiginal Array .
 */

function reverseArray(array) {
  var reversed = [];
  for (var i = 0; i < array.length; ++i) {
    reversed[i] = array[array.length - (i + 1)];
  }
  return reversed;
}

function reverseArrayInPlace(array) {
    var temp = 0;
    for (var i = 0; i < array.length; ++i) {
      temp = array[i];
      array[i] = array[array.length - (i + 1)];
      array[array.length - (i + 1)] = temp;
    }
  }
  // Test Case
var ar = [10, 9, 8, 7, 6];
console.log(reverseArray(ar));

// Reverse the Array
reverseArrayInPlace(ar);
console.log(ar);

The reverseArray() Function does it's job well , returning the reversed array , but the reverseArrayInPlace() is not working.

What did I do wrong ?

4 Answers 4

4

You are reversing it twice. Replace array.length with array.length/2

 function reverseArrayInPlace(array){
        var temp = 0;
        for (var i =0; i < array.length/2; ++i){
            temp = array[i];
            array[i] = array[array.length - (i+1)];
            array[array.length - ( i + 1 )] = temp; 
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Arun's answer shows the proper code to do what you're looking for.

The answer to why your code isn't working as expected is because you're looping through the entire array, swapping every variable with its mirror image. Once you've gone past halfway, though, your line of code that is setting the mirror image:

array[array.length - ( i + 1 )]

is swapping the array back into the original order.

Add this line just after your temp = array[i];line:

console.log('Swapping ' + array[i] + ' with ' + array[array.length - (i+1)]);

When you run your code now, you should immediately see why you need to stop at the halfway point.

You should see:

Swapping 10 with 6
Swapping 9 with 7
Swapping 8 with 8
Swapping 9 with 7
Swapping 10 with 6

Now, run with Arun's code and you'll see:

Swapping 10 with 6
Swapping 9 with 7
Swapping 8 with 8
[ 6, 7, 8, 9, 10 ]

Make sense?

2 Comments

You Learn something new everyday , even from such Blunders . :D
I've been programming for nearly 20 years, and I learn new stuff constantly.
0

If you're not too worried about optimizing your code and just learning then there's also another way to sort it and that's with the bubble sort.

function reverseArrayInPlace(arr){
  for (let i=0;i< arr.length;i++){
    for (let j=0;j< arr.length;j++){
      if (arr[j]<arr[j+1]) {
        let holder=arr[j]
        arr[j]=arr[j+1]
        arr[j+1]=holder
      }
    }
  }
  return arr;
}

console.log(reverseArrayInPlace([1,2,3,4,5])) //[5,4,3,2,1]

If you'd want to reverse an array like [10,9,8,7,6] just flip the less than sign to a greater than sign in the IF, e.g.

if(arr[j]>arr[j+1])

Comments

-1

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

var ar = [10, 9, 8, 7, 6];

console.log(reverseArrayInPlace(ar));

THe logic is not the correct, and not return nothing

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

var ar = [10, 9, 8, 7, 6];

console.log(reverseArrayInPlace(ar));

2 Comments

The specification says to not return anything and then alter the array.
if you want you dont need return yet something you can replace the return for a console.log(array) and it work

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.