Skip to main content

--description--

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"].

--instructions--

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.

--hints--

Your code should use the slice method.

assert(__helpers.removeJSComments(code).match(/\.slice/g));

The inputAnim variable should not change.

assert(
JSON.stringify(inputAnim) ===
JSON.stringify(['Cat', 'Dog', 'Tiger', 'Zebra', 'Ant'])
);

sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 3) should return ["Dog", "Tiger"].

assert(
JSON.stringify(sliceArray(['Cat', 'Dog', 'Tiger', 'Zebra', 'Ant'], 1, 3)) ===
JSON.stringify(['Dog', 'Tiger'])
);

sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 0, 1) should return ["Cat"].

assert(
JSON.stringify(sliceArray(['Cat', 'Dog', 'Tiger', 'Zebra', 'Ant'], 0, 1)) ===
JSON.stringify(['Cat'])
);

sliceArray(["Cat", "Dog", "Tiger", "Zebra", "Ant"], 1, 4) should return ["Dog", "Tiger", "Zebra"].

assert(
JSON.stringify(sliceArray(['Cat', 'Dog', 'Tiger', 'Zebra', 'Ant'], 1, 4)) ===
JSON.stringify(['Dog', 'Tiger', 'Zebra'])
);

--seed--

--seed-contents--

function sliceArray(anim, beginSlice, endSlice) {
// Only change code below this line


// Only change code above this line
}

const inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
sliceArray(inputAnim, 1, 3);

--solutions--

function sliceArray(anim, beginSlice, endSlice) {
return anim.slice(beginSlice, endSlice);
}
const inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];