Implementing Array.prototype.slice in JavaScript
The Array.prototype.slice() method is a fundamental JavaScript function used to extract a section of an array and return it as a new array. Implementing this method yourself is a great exercise in understanding array manipulation and prototype inheritance. This challenge asks you to recreate the core functionality of slice() without relying on the built-in version.
Problem Description
Your task is to implement a function mySlice that mimics the behavior of Array.prototype.slice(). mySlice should be a function that accepts an array and a variable number of arguments representing the start and end indices for the slice. It should return a new array containing a portion of the original array.
Key Requirements:
- New Array: The function must return a new array. Modifying the original array is not allowed.
- Start Index: The first argument represents the starting index (inclusive). If omitted, it defaults to 0.
- End Index: The second argument represents the ending index (exclusive). If omitted, it defaults to the length of the array.
- Index Handling:
- If the start index is greater than or equal to the array's length, return an empty array.
- If the end index is greater than the array's length, it should be treated as the array's length.
- If the start index is negative, it should be treated as
array.length + start index. If the resulting index is still negative or greater than or equal to the array length, return an empty array. - If the end index is negative, it should be treated as
array.length + end index. If the resulting index is negative, treat it as 0.
- Type Handling: The function should work correctly with arrays containing any data type.
Expected Behavior:
The function should behave identically to the native Array.prototype.slice() method in terms of extracting array elements and creating a new array.
Examples
Example 1:
Input: [1, 2, 3, 4, 5], 1, 3
Output: [2, 3]
Explanation: Extracts elements from index 1 (inclusive) up to index 3 (exclusive).
Example 2:
Input: [1, 2, 3, 4, 5]
Output: [1, 2, 3, 4, 5]
Explanation: Omitting start and end indices results in a copy of the entire array.
Example 3:
Input: [1, 2, 3, 4, 5], -2, 2
Output: [3, 4]
Explanation: -2 is treated as 5 - 2 = 3. Extracts elements from index 3 (inclusive) up to index 2 (exclusive), which is an empty slice.
Example 4:
Input: [1, 2, 3], 0, -1
Output: [1, 2]
Explanation: -1 is treated as 3 - 1 = 2. Extracts elements from index 0 (inclusive) up to index 2 (exclusive).
Example 5:
Input: [1, 2, 3], 2, 5
Output: [3]
Explanation: End index 5 is treated as the array length 3. Extracts elements from index 2 (inclusive) up to index 3 (exclusive).
Example 6:
Input: [1, 2, 3], 3, 3
Output: []
Explanation: Start index 3 is equal to the array length 3, so the slice is empty.
Constraints
- The input will always be an array.
- The start and end indices can be integers or omitted.
- The array can contain any data type.
- The function should be performant enough to handle arrays of up to 1000 elements without significant delays.
Notes
- Consider using a
forloop orwhileloop to iterate through the relevant portion of the array. - Pay close attention to the index calculations, especially when dealing with negative indices.
- Remember to create a new array to store the sliced elements. Do not modify the original array.
- Think about how to handle edge cases gracefully, such as empty arrays or invalid indices.
- This is a good opportunity to practice your understanding of JavaScript's array methods and index manipulation.