Created
July 18, 2020 15:48
-
-
Save imparvez/caac8143af438f89d0d2106b37fb8b61 to your computer and use it in GitHub Desktop.
Splitting an array into two.
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
// 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