Skip to content

Instantly share code, notes, and snippets.

@JoshuaRotimi
Created January 15, 2024 08:36
Show Gist options
  • Select an option

  • Save JoshuaRotimi/b31b256ef52507a3a6960fa82be9341a to your computer and use it in GitHub Desktop.

Select an option

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.
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