Last active
May 29, 2020 06:35
-
-
Save MinSomai/d87edbdc033dd6e22e62a75f0d202959 to your computer and use it in GitHub Desktop.
Basic Algorithm Scripting: Chunky Monkey
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
| function chunkArrayInGroups(arr, size) { | |
| let newArr = []; | |
| let arrLength = arr.length; | |
| let lastVal = arrLength % size; | |
| let canDo = parseInt(arrLength / size); | |
| let sliceStart = 0; | |
| let sliceEnd = size; | |
| if(lastVal == 0){ | |
| let i = 0; | |
| while(i < canDo){ | |
| let val = arr.slice(sliceStart, sliceEnd); | |
| sliceStart += size; | |
| sliceEnd += size; | |
| newArr.push(val); | |
| i++; | |
| } | |
| }else{ | |
| let i = 0; | |
| while(i < canDo){ | |
| let val = arr.slice(sliceStart, sliceEnd); | |
| sliceStart += size; | |
| sliceEnd += size; | |
| newArr.push(val); | |
| i++; | |
| } | |
| //push remaining values | |
| newArr.push(arr.slice(sliceStart, arrLength)); | |
| } | |
| return newArr; | |
| } | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment