Skip to content

Instantly share code, notes, and snippets.

@vasanthv
Created November 18, 2019 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasanthv/30b2bb5ce19495a0938e380a409b0560 to your computer and use it in GitHub Desktop.
Save vasanthv/30b2bb5ce19495a0938e380a409b0560 to your computer and use it in GitHub Desktop.
Answer to cassidoo's Interview question "Implement the substring() function."
String.prototype.mySubstring = function(start, end) {
let substr = '';
for (i = (start || 0); i <= ((end || this.length) - 1); i++) {
substr += this[i];
}
return substr;
}
console.log("hello world!".mySubstring(1, 5)); // => ello
console.log("hello world!".mySubstring(6)); // => world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment