Created
January 19, 2024 14:06
-
-
Save AngeloAnolin/5712d6bae230badfe243558a08a16d77 to your computer and use it in GitHub Desktop.
Flip 2D array vertically or horizontally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9] | |
] | |
const flip = (array, direction = 'HORIZONTAL') => { | |
const temp = [...array] | |
if (direction.toUpperCase() === 'VERTICAL') { | |
return (temp.reverse()) | |
} else { | |
return (temp.map(arr => arr.reverse())) | |
} | |
} | |
console.log('HOR', flip(array, 'HORIZONTAL')) // "HOR", [[3, 2, 1], [6, 5, 4], [9, 8, 7]] | |
console.log('VERT', flip(array, 'VERTICAL')) // "VERT", [[9, 8, 7], [6, 5, 4], [3, 2, 1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment