Reputation: 1012
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 ?
Upvotes: 1
Views: 400
Reputation: 59
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])
Upvotes: 0
Reputation: 1479
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));
Upvotes: -1
Reputation: 6588
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?
Upvotes: 1
Reputation: 7734
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;
}
}
Upvotes: 4