2018-10-10 18:03:03 -04:00
---
id: 587d78b2367417b2b2512b10
2021-02-06 04:42:36 +00:00
title: Remove Items Using splice()
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:14:21 +08:00
forumTopicId: 301166
2021-01-13 03:31:00 +01:00
dashedName: remove-items-using-splice
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Ok, so we've learned how to remove elements from the beginning and end of arrays using `shift()` and `pop()` , but what if we want to remove an element from somewhere in the middle? Or remove more than one element at once? Well, that's where `splice()` comes in. `splice()` allows us to do just that: **remove any number of consecutive elements** from anywhere in an array.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
`splice()` can take up to 3 parameters, but for now, we'll focus on just the first 2. The first two parameters of `splice()` are integers which represent indexes, or positions, of the array that `splice()` is being called upon. And remember, arrays are *zero-indexed* , so to indicate the first element of an array, we would use `0` . `splice()` 's first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete. For example:
2020-08-04 15:14:21 +08:00
```js
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
2021-02-06 04:42:36 +00:00
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
2020-08-04 15:14:21 +08:00
```
2021-02-06 04:42:36 +00:00
`splice()` not only modifies the array it's being called on, but it also returns a new array containing the value of the removed elements:
2020-08-04 15:14:21 +08:00
```js
let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);
2021-02-06 04:42:36 +00:00
// newArray equals ['really', 'happy']
2020-08-04 15:14:21 +08:00
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
We've initialized an array `arr` . Use `splice()` to remove elements from `arr` , so that it only contains elements that sum to the value of `10` .
2020-08-04 15:14:21 +08:00
2020-12-16 00:37:30 -07:00
# --hints--
2020-08-04 15:14:21 +08:00
2021-02-06 04:42:36 +00:00
You should not change the original line of `const arr = [2, 4, 5, 1, 7, 5, 2, 1];` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
assert(
__helpers.removeWhiteSpace(code).match(/constarr=\[2,4,5,1,7,5,2,1\];?/)
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`arr` should only contain elements that sum to `10` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert.strictEqual(
arr.reduce((a, b) => a + b),
10
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your code should utilize the `splice()` method on `arr` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
assert(__helpers.removeWhiteSpace(code).match(/arr\.splice\(/));
2020-12-16 00:37:30 -07:00
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The splice should only remove elements from `arr` and not add any additional elements to `arr` .
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert(
!__helpers.removeWhiteSpace(code).match(/arr\.splice\(\d+,\d+,\d+.*\)/g)
);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:14:21 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
// Only change code above this line
console.log(arr);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(1, 4);
```