0

I am trying to work through this problem and I am having trouble understanding why the function reverseArrayInPlace isn't doing what I want it to. The console.log in the function will return a reversed array, but when I return the same thing, I don't get the reversed array.

I am a beginner so please dumb down the answers alot. Thanks

function reverseArray(array){
  var x = [];
    for (i=array.length - 1; i>=0; i--){
    x.push(array[i]);
  }
  return x;
}

function reverseArrayInPlace(array){
  console.log(reverseArray(array));
  return reverseArray(array);
}
    

//console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]

Edit: Thanks for the replies. Can I get some feedback on this function:

 function reverseArrayInPlace(array){
  for (i=1; i<array.length; i++){
    var x = array[i];
    array.splice(i,1);
    array.unshift(x);
  }
  return array;
}

It seems to be working for me. Can you see anything wrong with it?

2
  • reverseArrayInPlace should actually be called createNewArrayWithValuesReversed (ie, the name of the method lies) Commented Feb 19, 2015 at 17:54
  • There's some good discussion on the same exercise here: stackoverflow.com/questions/27647032/… Commented Feb 19, 2015 at 17:59

3 Answers 3

1

You aren't reversing the array in place

In the function you are creating a new array and then adding the elements in reverse order

in the first line of your function you create a new variable

var x = [];

Unless you want to change your implementation to handle both cases in reverseArray you need to implement a different solution in the reverseArrayInPlace function

You will need to

  1. Determine the element to swap from the front
  2. Determine the element to swap from the back
  3. Know when you have reached the middle of the array and stop

You can optionally return the array if you would like to, but since the change is made in place you shouldn't need to

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

4 Comments

Actually I think it'll work if he just sets array = reverseArray(array); return array at the end of reverseArrayInPlace
You aren't actually reversing in place, var x = []; creates a new array, you are returning that at the end, so you aren't doing the change in place
to me 'in place' simply means 'the input object has been transformed into the output object at the end of the operation', not that there was no temporary array created
thats not in place, in place means modifying an existing object, you aren't transforming the object by using equal you are replacing the variable that previous lived at array with a new value (that the statement var x=[]; ) created
0

The reverseArrayInPlace() function isn't actually working in place. It is calling reverseArray() which is creating a new array to store the elements. As such, it needs to be returning this value.

Comments

0

why not just...

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

edit: nvm i see you are workign through a tutorial. imo this is the most elegant way though.

1 Comment

I made this mistake too, thought he wanted an elegant solution implementation, but wanted help through tutorial

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.