Alfredo Salzillo Posted on Apr 30, 2021 Can you hack this? #3 #challenge #fun #javascript Can you hack this? (3 Part Series) 1 Can you hack this? #1 2 Can you hack this? #2 3 Can you hack this? #3 Can you implement the String.prototype.split without using while, for, and other imperative cycles? Top comments (3) Subscribe Submit Preview Dismiss Collapse Expand '' '' '' Follow I Joined Mar 6, 2019 • Apr 30 '21 • Edited on Apr 30 • Edited Dropdown menu Copy link Hide String.prototype.split = function(delimiter, limit) { if (this.indexOf(delimiter) < 0) return [this.toString()] function splitHelper(str, del, _limit, acc) { if (str.length === 0 || limit === _limit) return acc if (del.length === 0) return [...str] const delIndex = str.indexOf(del) if (delIndex < 0) return splitHelper("", del, limit, [...acc, str]) return splitHelper(str.substring(delIndex + 1), del, _limit + 1, [...acc, str.substring(0, delIndex)]) } return splitHelper(this, delimiter, 0, []) } Enter fullscreen mode Exit fullscreen mode Collapse Expand IroncladDev IroncladDev IroncladDev Follow Opinionated Texan software engineer who aims to build and share great software with the world Email conner@ironclad.sh Location Dallas TX USA Education Homeschooled Joined Dec 15, 2020 • Apr 30 '21 Dropdown menu Copy link Hide Man, these are so fun!! You gotta post these more often. I am dead at this problem. I got as far as splitting the string into an array with [...string] but my brain broke after that. Collapse Expand Alfredo Salzillo Alfredo Salzillo Alfredo Salzillo Follow 🐺 Location Italy Pronouns He/Him Joined Nov 8, 2018 • May 1 '21 Dropdown menu Copy link Hide I will post more of this then 😊 Code of Conduct • Report abuse
Top comments (3)
Man, these are so fun!! You gotta post these more often.
I am dead at this problem. I got as far as splitting the string into an array with
[...string]but my brain broke after that.I will post more of this then 😊