refactor(curriculum): JavaScript Maps and Sets quiz and review (#58524)

Co-authored-by: Sem Bauke <sem@freecodecamp.org>
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
Kolade Chris 2025-02-03 18:14:47 +01:00 committed by GitHub
parent 2b3fe700c4
commit eb4b9715ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 29 deletions

View File

@ -33,29 +33,29 @@ A collection that does not allow deletion of its elements.
#### --answer--
A collection of unique values, where each value can only occur once.
A collection in which a value can only occur once.
### --question--
#### --text--
Which of the following is NOT a characteristic of a JavaScript Set?
Which of the following methods is available in Maps but not in Sets?
#### --distractors--
It stores elements in insertion order.
`has()`.
---
It only stores unique values.
`clear()`.
---
It allows iteration.
`values()`.
#### --answer--
It allows duplicate values.
`set()`.
### --question--
@ -105,28 +105,23 @@ How do you check if a Set contains a certain value?
#### --text--
What will the following code output?
```js
const mySet = new Set([1, 1, 2, 3]);
console.log(mySet.size);
```
Which of these is a great use case for Sets?
#### --distractors--
4
Managing key-value pairs in a database.
---
0
Creating ordered lists of items.
---
1
Storing hierarchical data structures in the browser local storage.
#### --answer--
3
Eliminating duplicates from an array.
### --question--
@ -148,7 +143,7 @@ A Set-like collection that prevents values from being removed.
#### --answer--
A Set-like collection that only stores objects and allows garbage collection.
A Set-like collection that only stores objects.
### --question--
@ -297,7 +292,7 @@ What does the `size` property of a Map return?
#### --distractors--
The number of unique keys in the Map.
The number of indexes in the Map.
---
@ -331,7 +326,7 @@ A Map-like collection that automatically sorts its keys.
#### --answer--
A Map-like collection with objects as keys that allows garbage collection.
A Map-like collection with weak references to the keys.
### --question--
@ -413,41 +408,41 @@ How can you iterate over the values in a Set?
#### --distractors--
Using `.forEach()`, or `.map()`.
By using the `.map()` method.
---
Using `.reduce()` or `.filter()`.
By using the `.reduce()` method.
---
Using `.keys()` or a loop.
By using the `.filter()` method.
#### --answer--
Using `.forEach()` or a loop.
By using the `.forEach()` method.
### --question--
#### --text--
What does `Map.prototype.get()` do?
Which two methods of Sets return a `SetIterator` that contains the values of a certain Set?
#### --distractors--
It retrieves all keys in the Map.
`entries()` and `values()`.
---
It sets a new key-value pair in the Map.
`forEach()` and `values()`.
---
It returns the size of the Map.
`has()` and `add()`.
#### --answer--
It retrieves the value for a specified key.
`keys()` and `values()`.
### --question--

View File

@ -26,6 +26,8 @@ console.log(set); // Set { 1, 2, 3, 4, 5 }
- `delete()`: Removes an element from the `Set`.
- `has()`: Checks if an element exists in the `Set`.
- `clear()`: Removes all elements from the `Set`.
- `keys()` and `values()`: Both returns a `SetIterator` that contains the values of the `Set`. They are the same because `keys()` is an alias for `values()`.
- `forEach()`: for iterating over the values of the `Set`.
## Weaksets in JavaScript
@ -60,13 +62,15 @@ console.log(map); // Map(3) { 'flower' => 'rose', 'fruit' => 'apple', 'vegetable
- `has()`: Checks if a key exists in the `Map`.
- `clear()`: Removes all key-value pairs from the `Map`.
Note that both Maps and Sets have the `size` property that returns the number of unique elements in them.
## WeakMaps in JavaScript
- A `WeakMap` is a collection of key-value pairs just like `Map`, but with weak references to the keys. The keys must be an object and the values can be anything you like.
## Maps vs WeakMaps
- WeakMaps are similar to WeakSets in that they only store objects and the references to those objects are "weak."
- WeakMaps are similar to WeakSets in that they only store objects and the references to those objects are "weak".
# --assignment--