Created
January 15, 2024 08:36
-
-
Save JoshuaRotimi/b31b256ef52507a3a6960fa82be9341a to your computer and use it in GitHub Desktop.
Given a 2D array, write a function that flips it vertically or horizontally.
This file contains hidden or 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 = (arr, type) => { | |
| let newList = []; | |
| if (type === 'horizontal') { | |
| for(const item of arr) { | |
| newList.push(item.slice().reverse()); | |
| } | |
| } | |
| if (type === 'vertical') { | |
| newList = [...arr].reverse(); | |
| } | |
| console.log(newList); | |
| return newList; | |
| }; | |
| flip(array, 'vertical') | |
| flip(array, 'horizontal') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment