From 6858070246f78fab77987037dc6fe4f621d8b79c Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Tue, 4 Mar 2025 18:10:20 +0100 Subject: [PATCH 1/5] Add description, syntax and examples for querySelectorAll() --- .../queryselectorall/queryselectorall.md | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md diff --git a/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md b/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md new file mode 100644 index 00000000000..24ad26f9dfd --- /dev/null +++ b/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md @@ -0,0 +1,86 @@ +--- +Title: '.querySelectorAll()' +Description: 'Selects multiple elements from the DOM that matches a specific CSS selector.' +Subjects: + - 'Code Foundations' + - 'Computer Science' + - 'Web development' + - 'Web design' + +Tags: + - 'Web API' + - 'Conceptual' + - 'DOM' + - 'ES6' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/computer-science' +--- + +The ``.querySelectorAll()`` method is a powerful tool in JavaScript for selecting multiple elements from the DOM that match a specified CSS selector. Unlike ``.querySelector()``, that returns only the first matching element, ``.querySelectorAll()`` returns a static NodeList containing all elements that match the given selector. + +This method is useful when you need to manipulate or interact with multiple elements at once, such as applying styles, adding event listeners, or updating content across several elements. + +## Syntax + + +```javascript +document.querySelectorAll(selector); +``` + +`selector` is a string containing one or more CSS selectors separated by commas. This can include any valid CSS selector, such as class names, IDs, element types, attributes, etc. + + +The `querySelectorAll` method returns a NodeList, which is a collection of nodes (elements) that match the specified selector(s). Note that a NodeList is not an array, but it can be iterated over using methods like forEach() or converted into an array using `Array.from()` +. +## Example + +Suppose you have the following HTML structure: + +```html + + + + Todolist + + + + + + +``` +You can use `.querySelectorAll()` to select all list items (`
  • `) with the class item and apply a style change to them: + + +```javascript +// Select all elements with the class "item" +const items = document.querySelectorAll('.item'); + +// Loop through the NodeList and apply a style change +items.forEach(item => { + item.style.color = 'blue'; +}); +``` + +In this example: + +`.querySelectorAll('.item')` selects all `
  • ` elements with the class item. + +The `forEach()` method is used to iterate over the NodeList and change the text color of each item to blue. + +You can also use more complex selectors. For instance, to select only the `
  • ` elements with both the item and completed classes: + +```javascript +const specialItem = document.querySelectorAll('.item.completed'); +specialItem.forEach(item => { + item.style.fontWeight = 'bold'; + item.style.textDecoration = 'underline'; +}); + +``` + \ No newline at end of file From b9b1580692d81e6d63e1d7e7af2290aacb60b055 Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Sat, 20 Dec 2025 01:02:37 +0100 Subject: [PATCH 2/5] delete query selector all --- .../querySelectorAll/querySelectorAll.md | 56 ------------ .../queryselectorall/queryselectorall.md | 86 ------------------- 2 files changed, 142 deletions(-) delete mode 100644 content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md delete mode 100644 content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md diff --git a/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md b/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md deleted file mode 100644 index 9d1e2157596..00000000000 --- a/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -Title: '.querySelectorAll()' -Description: 'Returns a static (non-live) NodeList of all elements in the document that match the given CSS selectors.' -Subjects: - - 'Code Foundations' - - 'Web Development' -Tags: - - 'Methods' - - 'Node' - - 'Selectors' -CatalogContent: - - 'introduction-to-javascript' - - 'paths/front-end-engineer-career-path' ---- - -In JavaScript, the **`.querySelectorAll()`** method under the `document` object returns a static (not live) `NodeList` of all elements that match the given group of [selectors](https://www.codecademy.com/resources/docs/css/selectors). - -## Syntax - -```pseudo -document.querySelectorAll(selectors); -``` - -- `selectors`: Represents a string containing one or more CSS selectors used to match elements in the document. It follows the same rules as CSS selectors and can include: - - Type selectors (`div`, `p`, `span`) - - Class selectors (`.class-name`) - - ID selectors (`#id-name`) - - Attribute selectors (`[type="text"]`, `[disabled]`) - - Combinations (`div p`, `.container > p`, `ul > li:first-child`) - -## Examples - -### Example 1 - -In this example, a `NodeList` of all `

    ` elements in the document is obtained: - -```js -const matches = document.querySelectorAll('p'); -``` - -### Example 2 - -The following example returns a list of all `

    ` elements in the document with a class of either `note` or `alert`: - -```js -const matches = document.querySelectorAll('div.note, div.alert'); -``` - -### Example 3 - -In this example, a list of `

    ` elements is obtained, whose immediate parent is a `

    ` with the class `highlighted`, and which are inside a container with the ID `test`: - -```js -const container = document.querySelector('#test'); -const matches = container.querySelectorAll('div.highlighted > p'); -``` diff --git a/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md b/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md deleted file mode 100644 index 24ad26f9dfd..00000000000 --- a/content/javascript/concepts/dom-manipulation/terms/queryselectorall/queryselectorall.md +++ /dev/null @@ -1,86 +0,0 @@ ---- -Title: '.querySelectorAll()' -Description: 'Selects multiple elements from the DOM that matches a specific CSS selector.' -Subjects: - - 'Code Foundations' - - 'Computer Science' - - 'Web development' - - 'Web design' - -Tags: - - 'Web API' - - 'Conceptual' - - 'DOM' - - 'ES6' -CatalogContent: - - 'introduction-to-javascript' - - 'paths/computer-science' ---- - -The ``.querySelectorAll()`` method is a powerful tool in JavaScript for selecting multiple elements from the DOM that match a specified CSS selector. Unlike ``.querySelector()``, that returns only the first matching element, ``.querySelectorAll()`` returns a static NodeList containing all elements that match the given selector. - -This method is useful when you need to manipulate or interact with multiple elements at once, such as applying styles, adding event listeners, or updating content across several elements. - -## Syntax - - -```javascript -document.querySelectorAll(selector); -``` - -`selector` is a string containing one or more CSS selectors separated by commas. This can include any valid CSS selector, such as class names, IDs, element types, attributes, etc. - - -The `querySelectorAll` method returns a NodeList, which is a collection of nodes (elements) that match the specified selector(s). Note that a NodeList is not an array, but it can be iterated over using methods like forEach() or converted into an array using `Array.from()` -. -## Example - -Suppose you have the following HTML structure: - -```html - - - - Todolist - - -
      -
    • Go to the gym
    • -
    • Read for two hours
    • -
    • Call Jerry
    • -
    • Take a nap
    • -
    - - - -``` -You can use `.querySelectorAll()` to select all list items (`
  • `) with the class item and apply a style change to them: - - -```javascript -// Select all elements with the class "item" -const items = document.querySelectorAll('.item'); - -// Loop through the NodeList and apply a style change -items.forEach(item => { - item.style.color = 'blue'; -}); -``` - -In this example: - -`.querySelectorAll('.item')` selects all `
  • ` elements with the class item. - -The `forEach()` method is used to iterate over the NodeList and change the text color of each item to blue. - -You can also use more complex selectors. For instance, to select only the `
  • ` elements with both the item and completed classes: - -```javascript -const specialItem = document.querySelectorAll('.item.completed'); -specialItem.forEach(item => { - item.style.fontWeight = 'bold'; - item.style.textDecoration = 'underline'; -}); - -``` - \ No newline at end of file From 7d71db35be3eaac0164e7dbf5f12e2ee7f8f8b60 Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Tue, 6 Jan 2026 14:15:42 +0100 Subject: [PATCH 3/5] Add cpp unordered sets reverse entry --- .../unordered-set/terms/reverse/reverse.md | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/reverse/reverse.md diff --git a/content/cpp/concepts/unordered-set/terms/reverse/reverse.md b/content/cpp/concepts/unordered-set/terms/reverse/reverse.md new file mode 100644 index 00000000000..ac0412f5f87 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/reverse/reverse.md @@ -0,0 +1,96 @@ +--- +Title: 'reverse()' +Description: 'Not supported for std::unordered_set; explains why and shows practical alternatives to iterate in reverse order.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Iterators' + - 'Sets' + - 'STL' + - 'Algorithms' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`reverse()`** operation is **not available** for [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). Unordered containers do not define a stable ordering of elements, so reverse iteration and reverse iterators are not provided. If you need to process elements in the opposite of some observed iteration, use an ordered container (like `std::set`) or copy the elements into a sequence container (like `std::vector`) and reverse that sequence. + +## Syntax + +- Not available: `std::unordered_set` has no member `reverse()` and no reverse iterators (`rbegin()`/`rend()`). + +Alternative using `std::reverse` on a sequence container: + +```pseudo +std::reverse(first, last); +``` + +**Parameters:** + +- `first` (BidirectionalIterator): Iterator to the beginning of the range. +- `last` (BidirectionalIterator): Iterator one past the end of the range. + +**Return value:** + +- `void` — reverses the elements in place within the given range. + +**Notes:** + +- Copying an `unordered_set` to a `std::vector` gives you a snapshot of its current internal iteration order; reversing that vector only reverses this unspecified order. For a predictable sorted order, use `std::set` (supports `rbegin()`/`rend()`). + +## Example + +This example copies elements from an `unordered_set` into a `std::vector`, then reverses the vector to iterate in the opposite order of the observed iteration: + +```cpp +#include +#include +#include +#include +using namespace std; + +int main() { + unordered_set numbers = {10, 5, 20, 15}; + + cout << "Forward-like iteration:\n"; + for (auto it = numbers.begin(); it != numbers.end(); ++it) { + cout << *it << "\n"; + } + + vector snapshot(numbers.begin(), numbers.end()); + reverse(snapshot.begin(), snapshot.end()); + + cout << "Reversed copy:\n"; + for (int x : snapshot) { + cout << x << "\n"; + } + + return 0; +} +``` + +## Codebyte Example + +This example demonstrates the same approach with strings: + +```codebyte/cpp +#include +#include +#include +#include +using namespace std; + +int main() { + unordered_set words = {"alpha", "beta", "gamma", "delta"}; + + vector snapshot(words.begin(), words.end()); + reverse(snapshot.begin(), snapshot.end()); + + for (const auto& w : snapshot) { + cout << w << "\n"; + } + + return 0; +} +``` From 71f7c86907da75cf36fce140606f16675f35b1a3 Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Tue, 6 Jan 2026 17:25:23 +0100 Subject: [PATCH 4/5] Update entry for cpp unordered set reserve entry --- .../unordered-set/terms/reserve/reserve.md | 103 ++++++++++++++++++ .../unordered-set/terms/reverse/reverse.md | 96 ---------------- 2 files changed, 103 insertions(+), 96 deletions(-) create mode 100644 content/cpp/concepts/unordered-set/terms/reserve/reserve.md delete mode 100644 content/cpp/concepts/unordered-set/terms/reverse/reverse.md diff --git a/content/cpp/concepts/unordered-set/terms/reserve/reserve.md b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md new file mode 100644 index 00000000000..5c3beb23f75 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md @@ -0,0 +1,103 @@ +--- +Title: 'reserve()' +Description: 'Requests capacity change for std::unordered_set so it can accommodate at least n elements without exceeding the maximum load factor.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Iterators' + - 'Sets' + - 'STL' + - 'Algorithms' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`reserve()`** method requests a capacity change for an [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). It sets the number of buckets to the amount needed to accommodate at least `n` elements without exceeding the container’s `max_load_factor()`. Calling `reserve(n)` may trigger a rehash; if it does, all iterators are invalidated, but references and pointers to elements remain valid. This operation does not sort or otherwise order elements. + +## Syntax + +```pseudo +unordered_set_name.reserve(n); +``` + +**Parameters:** + +- `n` (size_type): Minimum number of elements the container should be able to accommodate without exceeding `max_load_factor()`. + +**Return value:** + +- `void` + +**Notes:** + +- Effectively calls `rehash(ceil(n / max_load_factor()))`. +- May invalidate iterators if a rehash occurs; references and pointers to elements remain valid. +- Use `rehash(k)` to set the bucket count directly; use `reserve(n)` to plan for element count. +- Does not impose ordering; iteration order remains implementation-defined. + +## Example + +This example shows bucket capacity before and after `reserve()`, and demonstrates that elements remain accessible with the same unordered iteration semantics: + +```cpp +#include +#include +using namespace std; + +int main() { + unordered_set numbers = {10, 20, 30}; + + cout << "Buckets before: " << numbers.bucket_count() << "\n"; + + numbers.reserve(10); + + cout << "Buckets after: " << numbers.bucket_count() << "\n"; + cout << "Max load factor: " << numbers.max_load_factor() << "\n"; + cout << "Load factor: " << numbers.load_factor() << "\n"; + + for (int i = 1; i <= 7; ++i) { + numbers.insert(i * 5); + } + + cout << "Size: " << numbers.size() << "\n"; + cout << "Buckets final: " << numbers.bucket_count() << "\n"; + + return 0; +} +``` + +## Codebyte Example + +This example demonstrates `reserve()` with strings, and prints capacity-related information: + +```codebyte/cpp +#include +#include +#include +using namespace std; + +int main() { + unordered_set words = {"alpha", "beta", "gamma"}; + + cout << "Before: buckets=" << words.bucket_count() << ", size=" << words.size() << "\n"; + + words.reserve(10); + + cout << "After: buckets=" << words.bucket_count() << ", size=" << words.size() << "\n"; + + words.insert("delta"); + words.insert("epsilon"); + words.insert("zeta"); + + cout << "Load factor: " << words.load_factor() << "\n"; + cout << "Max load factor: " << words.max_load_factor() << "\n"; + + for (const auto& w : words) { + cout << w << "\n"; + } + + return 0; +} +``` diff --git a/content/cpp/concepts/unordered-set/terms/reverse/reverse.md b/content/cpp/concepts/unordered-set/terms/reverse/reverse.md deleted file mode 100644 index ac0412f5f87..00000000000 --- a/content/cpp/concepts/unordered-set/terms/reverse/reverse.md +++ /dev/null @@ -1,96 +0,0 @@ ---- -Title: 'reverse()' -Description: 'Not supported for std::unordered_set; explains why and shows practical alternatives to iterate in reverse order.' -Subjects: - - 'Code Foundations' - - 'Computer Science' -Tags: - - 'Iterators' - - 'Sets' - - 'STL' - - 'Algorithms' -CatalogContent: - - 'learn-c-plus-plus' - - 'paths/computer-science' ---- - -The **`reverse()`** operation is **not available** for [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). Unordered containers do not define a stable ordering of elements, so reverse iteration and reverse iterators are not provided. If you need to process elements in the opposite of some observed iteration, use an ordered container (like `std::set`) or copy the elements into a sequence container (like `std::vector`) and reverse that sequence. - -## Syntax - -- Not available: `std::unordered_set` has no member `reverse()` and no reverse iterators (`rbegin()`/`rend()`). - -Alternative using `std::reverse` on a sequence container: - -```pseudo -std::reverse(first, last); -``` - -**Parameters:** - -- `first` (BidirectionalIterator): Iterator to the beginning of the range. -- `last` (BidirectionalIterator): Iterator one past the end of the range. - -**Return value:** - -- `void` — reverses the elements in place within the given range. - -**Notes:** - -- Copying an `unordered_set` to a `std::vector` gives you a snapshot of its current internal iteration order; reversing that vector only reverses this unspecified order. For a predictable sorted order, use `std::set` (supports `rbegin()`/`rend()`). - -## Example - -This example copies elements from an `unordered_set` into a `std::vector`, then reverses the vector to iterate in the opposite order of the observed iteration: - -```cpp -#include -#include -#include -#include -using namespace std; - -int main() { - unordered_set numbers = {10, 5, 20, 15}; - - cout << "Forward-like iteration:\n"; - for (auto it = numbers.begin(); it != numbers.end(); ++it) { - cout << *it << "\n"; - } - - vector snapshot(numbers.begin(), numbers.end()); - reverse(snapshot.begin(), snapshot.end()); - - cout << "Reversed copy:\n"; - for (int x : snapshot) { - cout << x << "\n"; - } - - return 0; -} -``` - -## Codebyte Example - -This example demonstrates the same approach with strings: - -```codebyte/cpp -#include -#include -#include -#include -using namespace std; - -int main() { - unordered_set words = {"alpha", "beta", "gamma", "delta"}; - - vector snapshot(words.begin(), words.end()); - reverse(snapshot.begin(), snapshot.end()); - - for (const auto& w : snapshot) { - cout << w << "\n"; - } - - return 0; -} -``` From 361d7bc4c93f262730a44e0a71b617b55bc66f01 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 8 Jan 2026 12:43:18 +0530 Subject: [PATCH 5/5] Refine reserve() documentation for unordered_set Updated the description and notes for the reserve() function in std::unordered_set. Added details about return value and clarified usage. --- .../unordered-set/terms/reserve/reserve.md | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/reserve/reserve.md b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md index 5c3beb23f75..35f35f26d48 100644 --- a/content/cpp/concepts/unordered-set/terms/reserve/reserve.md +++ b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md @@ -1,14 +1,13 @@ --- Title: 'reserve()' -Description: 'Requests capacity change for std::unordered_set so it can accommodate at least n elements without exceeding the maximum load factor.' +Description: 'Requests capacity change for `std::unordered_set` so it can accommodate at least n elements without exceeding the maximum load factor.' Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'Iterators' + - 'Containers' - 'Sets' - 'STL' - - 'Algorithms' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' @@ -28,14 +27,7 @@ unordered_set_name.reserve(n); **Return value:** -- `void` - -**Notes:** - -- Effectively calls `rehash(ceil(n / max_load_factor()))`. -- May invalidate iterators if a rehash occurs; references and pointers to elements remain valid. -- Use `rehash(k)` to set the bucket count directly; use `reserve(n)` to plan for element count. -- Does not impose ordering; iteration order remains implementation-defined. +This method doesn't return anything (`void`). ## Example @@ -68,6 +60,17 @@ int main() { } ``` +The output of this code is: + +```shell +Buckets before: 3 +Buckets after: 11 +Max load factor: 1 +Load factor: 0.272727 +Size: 7 +Buckets final: 11 +``` + ## Codebyte Example This example demonstrates `reserve()` with strings, and prints capacity-related information: