---
# System prepended metadata

title: Transpose Array Coordinates

---

# Transpose Array Coordinates

## Explanation

The function `transposeArray()` takes in a 2d array. Each sub array contains 2
integers, a row integer and a column integer; the purpose of
`transposeArray()` is to swap the integers so that all row integers become column
integers and vice versa.

## Example

```javascript
[ [8, 9], [6, 7], [4, 5] ] // original array
[ [9, 8], [7, 6], [5, 4] ] // returned array
```

## Instructions

1. In your own words, please describe how this function works – what is the code
   doing in order to achieve the correct return statement.
2. Refactor the existing code to be more concise.
3. **BONUS:** Can you refactor to improve time & space complexities
    - Time Complexity: O(n^2) –> O(n)
    - Space Complexity: O(n) –> O(1)

```javascript
// [ rows, columns ]
let gameCoordinates = [
  [ 2, 1 ],
  [ 4, 3 ],
  [ 6, 5 ],
  [ 8, 7 ],
  [ 10, 9 ],
];

function transposeArray(array) {
  const newArray = [];

  for (let i = 0; i < array.length; i++) {
    newArray.push([]);

    for (let j = array[i].length - 1; j >= 0; j--) {
      newArray[i].push(array[i][j]);
    }
  };

  return newArray;
}

console.log(transposeArray(gameCoordinates));
// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ]
```
