Created
March 1, 2018 17:46
-
-
Save devNoiseConsulting/dd88a6a433861cb0098d17802c0ce2d8 to your computer and use it in GitHub Desktop.
Divide String - PhillyDev Slack #daily_programmer - 20180301
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
| const divideString = function(string, size) { | |
| const regex = new RegExp(`(.{${size}})`); | |
| let chunks = string.split(regex).filter(v => v); | |
| if (chunks[chunks.length - 1].length == size) { | |
| return chunks; | |
| } else { | |
| return [chunks.slice(0, -1), chunks[chunks.length - 1]]; | |
| } | |
| console.log(chunks); | |
| }; | |
| let test = 'phillydev'; | |
| let size = 3; | |
| let result = divideString(test, size); | |
| console.log(result); | |
| size = 2; | |
| result = divideString(test, size); | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment