Implementing the endsWith() Method for Strings
The built-in String.prototype.endsWith() method in JavaScript is a convenient way to check if a string ends with a specified substring. This challenge asks you to implement this functionality yourself, reinforcing your understanding of string manipulation and JavaScript's prototype system. This is a common interview question and a good exercise in understanding string methods.
Problem Description
You are tasked with creating a custom endsWith() method that can be added to the String.prototype. This method should take two arguments: the substring to search for (searchString) and an optional position argument. It should return true if the original string ends with the searchString, and false otherwise. The position argument, if provided, specifies the index within the original string from which to start the search. If the searchString is longer than the original string, or if the search fails, the method should return false.
Key Requirements:
- Prototype Extension: The solution must extend the
String.prototypeto add theendsWith()method. - Two Arguments: The method must accept two arguments:
searchString(the substring to search for) and an optionalposition(the starting index for the search). - Optional Position: The
positionargument should be optional. If not provided, the search should start from the end of the string. - Return Value: The method must return a boolean value (
trueorfalse). - Edge Case Handling: The method must handle edge cases such as:
searchStringbeing longer than the original string.positionbeing out of bounds (less than 0 or greater than or equal to the string length).- Empty
searchString.
Examples
Example 1:
Input: "Hello World".endsWith("World")
Output: true
Explanation: The string "Hello World" ends with the substring "World".
Example 2:
Input: "Hello World".endsWith("world")
Output: false
Explanation: The string "Hello World" does not end with the substring "world" (case-sensitive).
Example 3:
Input: "Hello World".endsWith("World", 5)
Output: false
Explanation: Starting the search at index 5, the string does not end with "World".
Example 4:
Input: "Hello World".endsWith("Hello World", 0)
Output: true
Explanation: Starting the search at index 0, the string ends with "Hello World".
Example 5:
Input: "Hello World".endsWith("World", 10)
Output: true
Explanation: Starting the search at index 10, the string ends with "World".
Example 6:
Input: "Hello".endsWith("Hello World")
Output: false
Explanation: The search string "Hello World" is longer than the original string "Hello".
Constraints
- The input string can contain any valid JavaScript string characters.
- The
searchStringcan be any valid JavaScript string. - The
positionargument, if provided, must be a non-negative integer. - The method should be implemented efficiently, avoiding unnecessary iterations. While performance isn't a primary focus, avoid excessively complex logic.
Notes
- Remember that string comparisons in JavaScript are case-sensitive.
- Consider using JavaScript's built-in string methods like
substring()orslice()to extract portions of the string for comparison. - Think carefully about how to handle the
positionargument and its impact on the search. The position is the index from which to start the search backwards. - Pay close attention to edge cases to ensure your implementation is robust.