2018-09-30 23:01:58 +01:00
---
id: 587d8254367417b2b2512c70
title: Create and Add to Sets in ES6
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301636
2021-01-13 03:31:00 +01:00
dashedName: create-and-add-to-sets-in-es6
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Now that you have worked through ES5, you are going to perform something similar in ES6. This will be considerably easier. ES6 contains a built-in data structure `Set` so many of the operations you wrote by hand are now included for you. Let's take a look:
2018-09-30 23:01:58 +01:00
To create a new empty set:
2020-11-27 19:02:05 +01:00
`var set = new Set();`
2018-09-30 23:01:58 +01:00
You can create a set with a value:
2020-11-27 19:02:05 +01:00
`var set = new Set(1);`
2018-09-30 23:01:58 +01:00
You can create a set with an array:
2020-11-27 19:02:05 +01:00
`var set = new Set([1, 2, 3]);`
Once you have created a set, you can add the values you wish using the `add` method:
2019-06-04 00:07:22 -07:00
```js
var set = new Set([1, 2, 3]);
set.add([4, 5, 6]);
```
2018-09-30 23:01:58 +01:00
As a reminder, a set is a data structure that cannot contain duplicate values:
2019-06-04 00:07:22 -07:00
```js
var set = new Set([1, 2, 3, 1, 2, 3]);
// set contains [1, 2, 3] only
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
For this exercise, return a set with the following values: `1, 2, 3, 'Taco', 'Cat', 'Awesome'`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your `Set` should only contain the values `1, 2, 3, Taco, Cat, Awesome` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
var test = checkSet();
return (
test.size == 6 & &
test.has(1) & &
test.has(2) & &
test.has(3) & &
test.has('Taco') & &
test.has('Cat') & &
test.has('Awesome')
);
})()
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function checkSet() {
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
2020-09-15 12:31:21 -07:00
// Only change code below this line
2018-10-08 01:01:53 +01:00
2020-09-15 12:31:21 -07:00
// Only change code above this line
2019-03-21 09:45:45 -06:00
console.log(Array.from(set));
2018-09-30 23:01:58 +01:00
return set;
}
checkSet();
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function checkSet(){var set = new Set([1,2,3,'Taco','Cat','Awesome']);
return set;}
```