Skip to content

Instantly share code, notes, and snippets.

@andrewbruner
Created December 30, 2022 16:08
Show Gist options
  • Save andrewbruner/92efd19641615dfb07d77a06a549d32f to your computer and use it in GitHub Desktop.
Save andrewbruner/92efd19641615dfb07d77a06a549d32f to your computer and use it in GitHub Desktop.
Clone of String.prototype.split()
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