wip: add more questions

chore/javascript
Arik Chakma 1 year ago
parent 56473b129c
commit 50e26e4fe2
  1. 55
      src/data/question-groups/javascript/content/find-unique-array-values.md
  2. 19
      src/data/question-groups/javascript/content/map.md
  3. 14
      src/data/question-groups/javascript/content/set.md
  4. 15
      src/data/question-groups/javascript/javascript.md

@ -0,0 +1,55 @@
There are serveral ways to find unique values in an array. Here are some of them:
## Using `Set`
```js
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript'];
const uniqueRoadmaps = [...new Set(roadmaps)];
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js']
```
## Using `filter()`
```js
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript'];
const uniqueRoadmaps = roadmaps.filter(
(roadmap, index) => roadmaps.indexOf(roadmap) === index
);
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js']
```
## Using `reduce()`
```js
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript'];
const uniqueRoadmaps = roadmaps.reduce((unique, roadmap) => {
return unique.includes(roadmap) ? unique : [...unique, roadmap];
}, []);
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js']
```
## Using `forEach()`
```js
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript'];
const uniqueRoadmaps = [];
roadmaps.forEach((roadmap) => {
if (!uniqueRoadmaps.includes(roadmap)) {
uniqueRoadmaps.push(roadmap);
}
});
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js']
```
## Using `for...of`
```js
const roadmaps = ['JavaScript', 'React', 'Node.js', 'Node.js', 'JavaScript'];
const uniqueRoadmaps = [];
for (const roadmap of roadmaps) {
if (!uniqueRoadmaps.includes(roadmap)) {
uniqueRoadmaps.push(roadmap);
}
}
console.log(uniqueRoadmaps); // ['JavaScript', 'React', 'Node.js']
```

@ -0,0 +1,19 @@
Map is another data structure in JavaScript which is similar to `Object` but the key can be of any type. It is a collection of elements where each element is stored as a Key, value pair. It is also known as a Hash table or a dictionary.
The `key` can be of any type but the `value` can be of any type. The `key` is unique and immutable, whereas the `value` can be mutable or immutable.
```js
const roadmap = new Map();
roadmap.set('name', 'JavaScript');
roadmap.set('type', 'dynamic');
roadmap.set('year', 1995);
console.log(roadmap.get('name')); // JavaScript
roadmap.delete('year');
console.log(roadmap.has('year')); // false
console.log(roadmap.size); // 2
roadmap.clear();
console.log(roadmap.size); // 0
```

@ -0,0 +1,14 @@
Set is another data structure in JavaScript which is similar to `Array` but the values are unique. It is a collection of elements where each element is stored as a value without any keys.
```js
const roadmap = new Set();
roadmap.add('JavaScript');
roadmap.add('dynamic');
roadmap.add(1995);
console.log(roadmap.has('JavaScript')); // true
roadmap.delete(1995);
console.log(roadmap.has(1995)); // false
console.log(roadmap.size); // 2
```

@ -113,4 +113,19 @@ questions:
topics:
- 'OOP'
- 'Advanced'
- question: What is Map in JavaScript?
answer: map.md
topics:
- 'Core'
- 'Beginner'
- question: What is Set in JavaScript?
answer: set.md
topics:
- 'Core'
- 'Beginner'
- question: How you can find unique values in an array?
answer: find-unique-array-values.md
topics:
- 'Core'
- 'Intermediate'
---

Loading…
Cancel
Save