Skip to content

Instantly share code, notes, and snippets.

@michaelwclark
Created March 29, 2018 21:36
Show Gist options
  • Select an option

  • Save michaelwclark/fa6ec931791ef935a0559c6216e5c7bb to your computer and use it in GitHub Desktop.

Select an option

Save michaelwclark/fa6ec931791ef935a0559c6216e5c7bb to your computer and use it in GitHub Desktop.
Rotate 2D Array
export default const rotate2dArray = array => {
const resultLength = array[0].length
const resultArray = Array(resultLength).fill([])
return resultArray.map((x, widthIndex) =>
array.map(innerArray => innerArray[widthIndex])
)
}
import rotate2dArray from './rotate2dArray'
describe('rotate2dArray', () => {
it('should rotate array 90 degrees', () => {
const input = [[1, 2, 3], [4, 5, 6]]
const expected = [[1, 4], [2, 5], [3, 6]]
expect(rotate2dArray(input)).toEqual(expected)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment