2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Add Items Using splice()
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Add Items Using splice()
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
2018-10-12 15:37:13 -04:00
|
|
|
- Using the splice() function, you must remove the first 2 elements from array `arr` and replace them with `DarkSalmon` and `BlanchedAlmond`.
|
|
|
|
- Remember the splice function can take up to three parameters.
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Example:
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
arr.splice(0, 1, "Two");
|
|
|
|
/* The first two paramemters are the same as they were in the previous challenge.
|
|
|
|
`0` will start the `splice()` function off at index 0.
|
|
|
|
The second parameter `1` will remove only 1 variable from the array.
|
|
|
|
The final variable "Two" will replace the variable in arr[0].
|
|
|
|
Note: The final parameter can take more than 1 arguement.
|
|
|
|
*/
|
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
```javascript
|
|
|
|
function htmlColorNames(arr) {
|
|
|
|
// change code below this line
|
|
|
|
arr.splice(0, 2, "DarkSalmon", "BlanchedAlmond");
|
|
|
|
// change code above this line
|
|
|
|
return arr;
|
2019-07-24 00:59:27 -07:00
|
|
|
}
|
|
|
|
|
2018-10-12 15:37:13 -04:00
|
|
|
// do not change code below this line
|
2019-07-24 00:59:27 -07:00
|
|
|
console.log(
|
|
|
|
htmlColorNames([
|
|
|
|
"DarkGoldenRod",
|
|
|
|
"WhiteSmoke",
|
|
|
|
"LavenderBlush",
|
|
|
|
"PaleTurqoise",
|
|
|
|
"FireBrick"
|
|
|
|
])
|
|
|
|
);
|
2018-10-12 15:37:13 -04:00
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|