From 6858070246f78fab77987037dc6fe4f621d8b79c Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Tue, 4 Mar 2025 18:10:20 +0100 Subject: [PATCH 1/4] 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/4] 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 091eb2ce0a2dc5557976beeb8273e485413f1488 Mon Sep 17 00:00:00 2001 From: Adedeji Agunbiade Date: Wed, 11 Feb 2026 22:27:17 +0100 Subject: [PATCH 3/4] Feat: add pytouch tensor operations trunc term --- .../tensor-operations/terms/trunc/trunc.md | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md diff --git a/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md b/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md new file mode 100644 index 00000000000..11f3ce8dab5 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md @@ -0,0 +1,96 @@ +--- +Title: '.trunc()' +Description: 'Removes the fractional part of each element by truncating toward zero.' +Subjects: + - 'AI' + - 'Data Science' +Tags: + - 'AI' + - 'Arrays' + - 'Data Structures' + - 'Deep Learning' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/computer-science' +--- + +In PyTorch, **`.trunc()`** removes the decimal part of each element and keeps only the integer part, moving values toward zero. + +- Positive numbers: 1.9 → 1.0 +- Negative numbers: -2.7 → -2.0 +- It does not round; it simply cuts off the fraction. + +## Syntax + +```pseudo +torch.trunc(input, out=None) +``` + +- `input`: The input tensor. +- `out`: Optional output tensor to write results into. + +Alternative tensor method: + +```pseudo +tensor.trunc() +``` + +**Return value:** + +Returns a tensor of the same dtype as `input`, with fractional parts removed (toward zero). + +## Examples + +Example 1: Basic truncation on 1D tensor + +```py +import torch + +x = torch.tensor([1.9, -2.1, 0.0, 3.5]) +y = torch.trunc(x) + +print(y) +``` + +The above code will result in the following output: + +```shell +tensor([ 1., -2., 0., 3.]) +``` + +Example 2: Using the out parameter + +```py +import torch + +a = torch.tensor([3.4742, 0.5466, -0.8008, -0.9079]) +out = torch.empty_like(a) +torch.trunc(a, out=out) + +print(out) +``` + +A sample output might be: + +```shell +tensor([ 3., 0., -0., -0.]) +``` + +Example 3: Method form on a 2D tensor + +```py +import torch + +m = torch.tensor([[ 2.8, -1.2, 0.4], + [-3.9, 7.1, -0.5]]) +n = m.trunc() + +print(n) +``` + +The above code will result in the following output: + +```shell +tensor([[ 2., -1., 0.], + [-3., 7., -0.]]) +``` From 926cc8918f385e8eeabc92d66d47c118179a9592 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 12 Feb 2026 13:28:29 +0530 Subject: [PATCH 4/4] Revise .trunc() method documentation for clarity Updated the description and examples for the .trunc() method in PyTorch documentation, clarifying behavior with integer tensors and enhancing example explanations. --- .../tensor-operations/terms/trunc/trunc.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md b/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md index 11f3ce8dab5..109b3e95ce6 100644 --- a/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md +++ b/content/pytorch/concepts/tensor-operations/terms/trunc/trunc.md @@ -14,11 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In PyTorch, **`.trunc()`** removes the decimal part of each element and keeps only the integer part, moving values toward zero. - -- Positive numbers: 1.9 → 1.0 -- Negative numbers: -2.7 → -2.0 -- It does not round; it simply cuts off the fraction. +In PyTorch, **`.trunc()`** removes the fractional component of each element in a tensor by truncating values toward zero. If the input tensor has an integer dtype, the values remain unchanged. ## Syntax @@ -26,22 +22,24 @@ In PyTorch, **`.trunc()`** removes the decimal part of each element and keeps on torch.trunc(input, out=None) ``` -- `input`: The input tensor. -- `out`: Optional output tensor to write results into. - Alternative tensor method: ```pseudo tensor.trunc() ``` +**Parameters:** + +- `input`: The input tensor. +- `out` (optional): Output tensor to write results into. + **Return value:** -Returns a tensor of the same dtype as `input`, with fractional parts removed (toward zero). +Returns a tensor of the same dtype as `input`, with fractional parts removed (toward zero). Floating-point tensors are truncated, while integer tensors are unaffected. -## Examples +## Example 1: Basic truncation on 1D tensor -Example 1: Basic truncation on 1D tensor +In this example, `torch.trunc()` removes the fractional parts of values in a 1D tensor, truncating each number toward zero: ```py import torch @@ -58,7 +56,9 @@ The above code will result in the following output: tensor([ 1., -2., 0., 3.]) ``` -Example 2: Using the out parameter +## Example 2: Using the `out` parameter + +In this example, `torch.trunc()` uses the `out` parameter to store the truncated results in a preallocated tensor: ```py import torch @@ -76,7 +76,9 @@ A sample output might be: tensor([ 3., 0., -0., -0.]) ``` -Example 3: Method form on a 2D tensor +## Example 3: Method form on a 2D tensor + +In this example, the `.trunc()` tensor method truncates all elements of a 2D tensor toward zero: ```py import torch @@ -84,7 +86,6 @@ import torch m = torch.tensor([[ 2.8, -1.2, 0.4], [-3.9, 7.1, -0.5]]) n = m.trunc() - print(n) ```