Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created July 18, 2020 15:48
Show Gist options
  • Save imparvez/caac8143af438f89d0d2106b37fb8b61 to your computer and use it in GitHub Desktop.
Save imparvez/caac8143af438f89d0d2106b37fb8b61 to your computer and use it in GitHub Desktop.
Splitting an array into two.
// Splitting an array into two.
// Use case:
// Given array: [1,2,3,4,5]
// Expected result: [1, 2] and [3, 4, 5]
var arr = [1, 2, 3, 4, 5]
var arrLength = arr.length
var arrMidSection = Math.floor(arrLength/2)
var leftPart = arr.slice(0, arrMidSection)
var rightPart = arr.slice(arrMidSection)
// leftPart
// (2)[1, 2]
// rightPart
// (3)[3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment