Skip to content

Instantly share code, notes, and snippets.

@rxluz
Last active January 29, 2019 04:50
Show Gist options
  • Save rxluz/12398640691c1eeea6af6dc6b974b475 to your computer and use it in GitHub Desktop.
Save rxluz/12398640691c1eeea6af6dc6b974b475 to your computer and use it in GitHub Desktop.
JS Data Structures: Arrays, see more at: https://medium.com/p/e1bc57bda950
const animals = ['elephant', 'monkey', 'snake', 'lion', 'zebra']
const reverseArray = currentArray => {
for (let index = 0; index < currentArray.length / 2; index++) {
const currentItem = currentArray[index]
const otherItem = currentArray[currentArray.length - 1 - index]
currentArray[index] = otherItem
currentArray[currentArray.length - 1 - index] = currentItem
}
return currentArray
}
const animalsReversed = reverseArray(animals)
console.log(animalsReversed) // will return [ 'zebra', 'lion', 'snake', 'monkey', 'elephant' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment