Return Part of an Array Using the slice Method
The slice
method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it's non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice
method does not mutate the original array, but returns a new one.
Here's an example:
const arr = ["Cat", "Dog", "Tiger", "Zebra"];
const newArray = arr.slice(1, 3);
newArray
would have the value ["Dog", "Tiger"]
.
Use the slice
method in the sliceArray
function to return part of the anim
array given the provided beginSlice
and endSlice
indices. The function should return an array.
Tests
- Waiting: 1. Your code should use the
slice
method. - Waiting: 2. The
inputAnim
variable should not change. - Waiting: 3.
sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 3)
should return["Dog", "Tiger"]
. - Waiting: 4.
sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 0, 1)
should return["Cat"]
. - Waiting: 5.
sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 4)
should return["Dog", "Tiger", "Zebra"]
.
/** * Your test output will go here */