0

I have following problem:

// Reverse Array

Write a function that accepts an array and reverses that array in place. The behavior should mimic the behavior of the native .reverse() array method. However, your reverse function should accept the array to operate on as an argument, rather than being invoked as a method on that array.

Do not use the native .reverse() method in your own implementation.

I tried the following code:

let myArray = [1, 2, 3, 4];


function reverse(myArray) {

  let newArray = []; 

  // pop all of elements from roginal array, and store in new array

  for (i=myArray.length-1; i>=0; i--){
    newArray.push(myArray[i])

    console.log(newArray)
  }

  while (newArray.length){

    myArray.unshift(newArray)
  }


  return myArray; 
}


reverse(myArray);
console.log(myArray) // expected output is [4, 3, 2, 1]

My code just keeps running and no console.log output is produced. Notice I want the reverse done to the input array argument.

What am I doing wrong? Also, what does while (newArray.length) mean / what is it doing conceptually?

5
  • 2
    You've missed an important part of the requirement: "...and reverses that array in place" Commented Mar 30, 2019 at 16:34
  • 1
    and the reason you code "just keeps running" is your infinite loop - nothing inside that loop while (newArray.length) changes the length of newArray Commented Mar 30, 2019 at 16:36
  • To reverse an array in place you just swap the first and last element, and then the second and second-to-last element, etc, stopping when you get half way. Commented Mar 30, 2019 at 16:37
  • I would also suggest swapping. Swap the i th element with length-i element. Commented Mar 30, 2019 at 16:56
  • 1
    You have selected an answer that will cause you to fail your assignment. Commented Mar 31, 2019 at 7:50

6 Answers 6

0

Not sure why you need the unshift you can just iterate and return the array where you are pushing the value

let myArray = [1, 2, 3, 4];

function reverse(myArray) {
  let newArray = [];
  for (i = myArray.length - 1; i >= 0; i--) {
    newArray.push(myArray[i])
  }
  return newArray;
}
console.log(reverse(myArray))

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

1 Comment

This is not an in-place reversal!
0

You can iterate the array 'till the middle, and switch between the current (i) and the opposite (length - i - 1):

const myArray = [1, 2, 3, 4];

function reverse(myArray) {
  const length = myArray.length;
  const middle = Math.floor(length / 2);
  
  for(let i = 0; i < middle; i++) {
    let tmp = myArray[i];
    myArray[i] = myArray[length - i - 1];
    myArray[length - i - 1] = tmp;
  }
}


reverse(myArray);
console.log(myArray) // expected output is [4, 3, 2, 1]

Comments

0

You can swap first and last element in an array and iteratively swap the next and prev respectively.

You don't have to visit the complete set in the loop, get the middle element and rotate the index around that

function reverseInArray(arr){
  let len = arr.length;
  let temp;
  for(let i=0; i < len/2; i++){
  	temp = arr[i];
    arr[i] = arr[len - i - 1];
    arr[len - i - 1] = temp;
  }  
  return arr;
}

console.log(reverseInArray([1,2,3,4,5]));

Comments

0

You could swap the first and the last element and start from the most inner item.

function reverse(array) {
    var i = array.length >> 1, // take half of the length as integer
        l = array.length - 1;  // last index value to calculate the other side

    while (i--) [array[i], array[l - i]] = [array[l - i], array[i]];
}

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

reverse(a);
console.log(...a);

1 Comment

interesting use of structured assignment to achieve the element swap :)
0

Just swap pairs starting at either end of the array, until there's none left:

function reverse(a) {
    for (let i = 0, j = a.length - 1; i < j; ++i, --j) {
        let tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    return a;  // not required, but allows use in an expression
}

In ES2016 you can use destructuring assignments to perform the swap in one operation without the use of a temporary variable:

function reverse(a) {
    for (let i = 0, j = a.length - 1; i < j; ++i, --j) {
        [ a[j], a[i] ] = [ a[i], a[j] ];
    }
    return a;
}

Comments

-1

Here:

  while (newArray.length){

    myArray.unshift(newArray)
  }

You're adding to myArray, but not taking from newArray, hence infinite loop. Methinks it should be myArray.unshift(newArray.pop()).

Comments

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.