Created
November 18, 2019 08:19
-
-
Save vasanthv/30b2bb5ce19495a0938e380a409b0560 to your computer and use it in GitHub Desktop.
Answer to cassidoo's Interview question "Implement the substring() function."
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
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