Added solution for "Create and Add to Sets in ES6" (#27554)
* Added solution for "Create and Add to Sets in ES6" * add full solution * Remove notes
This commit is contained in:
committed by
The Coding Aviator
parent
21dd71a27d
commit
b2fa8896ba
@ -3,8 +3,58 @@ title: Create and Add to Sets in ES6
|
||||
---
|
||||
## Create and Add to Sets in ES6
|
||||
|
||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-and-add-to-sets-in-es6/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
||||
|
||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
###  Problem Explanation:
|
||||
|
||||
To solve this problem, you have to add an array of items to the set.
|
||||
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
Use the add function to add an array of strings to the set.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
##  Hint: 2
|
||||
|
||||
Use the length attribute on the values of the Set.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
|
||||
|
||||
## Spoiler Alert!
|
||||
|
||||

|
||||
|
||||
**Solution ahead!**
|
||||
|
||||
##  Basic Code Solution:
|
||||
|
||||
```js
|
||||
function checkSet() {
|
||||
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
|
||||
set.add("Taco");
|
||||
set.add("Cat");
|
||||
set.add("Awesome");
|
||||
console.log(Array.from(set));
|
||||
return set;
|
||||
}
|
||||
|
||||
checkSet();
|
||||
```
|
||||
|
||||
 <a href='https://repl.it/repls/HighlevelImperfectDiscussion' target='_blank' rel='nofollow'>Run Code</a>
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
* Creating a set object as shown in pre-written code will create the set without duplicate objects.
|
||||
* Therefore, by using the add function, we can add items to the set and they will not be duplicated, and will still be represented in the array.
|
||||
|
||||
##  NOTES FOR CONTRIBUTIONS:
|
||||
|
||||
*  **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
|
||||
* Add an explanation of your solution.
|
||||
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. 
|
||||
|
Reference in New Issue
Block a user