Created
December 30, 2022 16:08
-
-
Save andrewbruner/92efd19641615dfb07d77a06a549d32f to your computer and use it in GitHub Desktop.
Clone of String.prototype.split()
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
function split(input, char) { | |
let arr = [ ]; | |
let prevIdx = 0; | |
for (let i = 0; i < input.length; i++) { | |
if (input[i] === char) { | |
let item = ''; | |
for (let j = prevIdx; j < i; j++) { | |
item += input[j]; | |
} | |
arr = [ ...arr, item, ]; | |
prevIdx = i + 1; | |
} | |
} | |
let item = ''; | |
for (let j = prevIdx; j < input.length; j++) { | |
item += input[j]; | |
} | |
arr = [ ...arr, item, ]; | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment