chunk_id stringlengths 36 36 | source stringclasses 35
values | source_url stringlengths 0 290 | upstream_license stringclasses 1
value | document_id stringlengths 36 36 | chunk_index int64 0 324k | retrieved_at stringclasses 2
values | chunker_version stringclasses 4
values | content_hash stringlengths 15 64 | content stringlengths 50 44.7k | namespace stringclasses 9
values | source_name stringclasses 35
values | raw_text stringlengths 50 44.7k | cleaned_text stringlengths 50 44.7k | tags stringclasses 49
values | collection_name stringclasses 11
values |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3450ef31-7546-42f1-9237-b92cd65954f5 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/jest-mock-global-methods.md | unknown | acd7827a-06f1-44ef-930e-7816f5e2a77f | 0 | SemanticChunker@1.0.0 | 7175fb669b26f8d8d1cc2809d4c9ce95a865034d610b0724ed560daef048b5b4 | ---
title: Mocking global object methods in Jest
language: javascript
tags: [testing]
cover: trippy-chemicals
excerpt: Testing your code is important, but mocking can be tricky at times. Here's a quick guide on how to mock global object methods in Jest.
listed: true
dateModified: 2022-03-27
---
Testing is a big part o... | unknown | unknown | ---
title: Mocking global object methods in Jest
language: javascript
tags: [testing]
cover: trippy-chemicals
excerpt: Testing your code is important, but mocking can be tricky at times. Here's a quick guide on how to mock global object methods in Jest.
listed: true
dateModified: 2022-03-27
---
Testing is a big part o... | ---
title: Mocking global object methods in Jest
language: javascript
tags: [testing]
cover: trippy-chemicals
excerpt: Testing your code is important, but mocking can be tricky at times. Here's a quick guide on how to mock global object methods in Jest.
listed: true
dateModified: 2022-03-27
---
Testing is a big part o... | code_snippets | ||
ef714c42-6475-4d0a-871b-6ed90332a409 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/join-url-segments.md | unknown | 69069938-fcb0-4b20-a41b-ac4fcf725b32 | 0 | SemanticChunker@1.0.0 | 0bbb31f950633e3baf66ecefee3f73336d5471969dd70cd98da8e244b7535d48 | ---
title: Join the segments of a URL using JavaScript
shortTitle: Join URL segments
language: javascript
tags: [string,regexp]
cover: digital-nomad-2
excerpt: Learn how to join the segments of a URL and normalize the resulting URL using JavaScript.
listed: true
dateModified: 2024-08-10
---
When building URLs programm... | unknown | unknown | ---
title: Join the segments of a URL using JavaScript
shortTitle: Join URL segments
language: javascript
tags: [string,regexp]
cover: digital-nomad-2
excerpt: Learn how to join the segments of a URL and normalize the resulting URL using JavaScript.
listed: true
dateModified: 2024-08-10
---
When building URLs programm... | ---
title: Join the segments of a URL using JavaScript
shortTitle: Join URL segments
language: javascript
tags: [string,regexp]
cover: digital-nomad-2
excerpt: Learn how to join the segments of a URL and normalize the resulting URL using JavaScript.
listed: true
dateModified: 2024-08-10
---
When building URLs programm... | code_snippets | ||
19ba6ab1-86f4-4fa2-9de9-a072ac32c748 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterate-object-properties.md | unknown | 3d6fc22a-db31-40a1-8df2-aa413d29294a | 0 | SemanticChunker@1.0.0 | 09c8a6aca887de5c3de95b311969fd892bb069a91406e395211725ca88e2a78f | ---
title: Iterate over a JavaScript object's own properties
shortTitle: Iterate over object properties
language: javascript
tags: [object]
cover: building-facade
excerpt: Iterate over all own properties of an object, running a callback for each one.
listed: true
dateModified: 2024-01-16
---
`Array.prototype.forEach()... | unknown | unknown | ---
title: Iterate over a JavaScript object's own properties
shortTitle: Iterate over object properties
language: javascript
tags: [object]
cover: building-facade
excerpt: Iterate over all own properties of an object, running a callback for each one.
listed: true
dateModified: 2024-01-16
---
`Array.prototype.forEach()... | ---
title: Iterate over a JavaScript object's own properties
shortTitle: Iterate over object properties
language: javascript
tags: [object]
cover: building-facade
excerpt: Iterate over all own properties of an object, running a callback for each one.
listed: true
dateModified: 2024-01-16
---
`Array.prototype.forEach()... | code_snippets | ||
3e7859ae-e1d9-4bb1-9976-4872ea872464 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterate-object-properties.md | unknown | 3d6fc22a-db31-40a1-8df2-aa413d29294a | 2 | SemanticChunker@1.0.0 | aac2281a98b2e0967d0670c07a90ce37bb7912d7f554fcf728af7c42bf3bbcf0 | [Iterate over object properties > Iterate over object properties in reverse]
## Iterate over object properties in reverse
If you need to iterate over the object properties in reverse, you can use `Array.prototype.reverse()` to reverse the order of the keys before iterating over them.
```js
const forOwnRight = (obj, ... | unknown | unknown | [Iterate over object properties > Iterate over object properties in reverse]
## Iterate over object properties in reverse
If you need to iterate over the object properties in reverse, you can use `Array.prototype.reverse()` to reverse the order of the keys before iterating over them.
```js
const forOwnRight = (obj, ... | [Iterate over object properties > Iterate over object properties in reverse]
## Iterate over object properties in reverse
If you need to iterate over the object properties in reverse, you can use `Array.prototype.reverse()` to reverse the order of the keys before iterating over them.
```js
const forOwnRight = (obj, ... | code_snippets | ||
e2d32577-fca3-4ebe-b6e5-1d8ad8aa3001 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterate-object-properties.md | unknown | 3d6fc22a-db31-40a1-8df2-aa413d29294a | 1 | SemanticChunker@1.0.0 | cb2513d525285901a241118f908da30aa5d64d36d6430ddef70fca0e473978d0 | [Iterate over object properties]
## Iterate over object properties
As mentioned already, we simply need to use `Object.keys()` to get all the properties of the object and then use `Array.prototype.forEach()` to run the provided function for each key-value pair. The callback should receive **three arguments** - the va... | unknown | unknown | [Iterate over object properties]
## Iterate over object properties
As mentioned already, we simply need to use `Object.keys()` to get all the properties of the object and then use `Array.prototype.forEach()` to run the provided function for each key-value pair. The callback should receive **three arguments** - the va... | [Iterate over object properties]
## Iterate over object properties
As mentioned already, we simply need to use `Object.keys()` to get all the properties of the object and then use `Array.prototype.forEach()` to run the provided function for each key-value pair. The callback should receive **three arguments** - the va... | code_snippets | ||
26d9e2c0-cb80-4d9b-825d-0d1ee4e96587 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterators.md | unknown | 55ce89c8-92d0-4d08-871a-dafdecebd9b7 | 2 | SemanticChunker@1.0.0 | 505b326c62acdf3686160ddc6f00d1bf0f2c0880eb72469bcba2c6b1bd1eff10 | In this example, we use the native array iterator of the `data` object to make our `SpecialList` iterable, returning the exact values of the `data` array. Meanwhile, we also define a `values` method, which is an iterator itself, using `Array.prototype.filter()` and `Array.prototype.map()` on the `data` array. Finally, ... | unknown | unknown | In this example, we use the native array iterator of the `data` object to make our `SpecialList` iterable, returning the exact values of the `data` array. Meanwhile, we also define a `values` method, which is an iterator itself, using `Array.prototype.filter()` and `Array.prototype.map()` on the `data` array. Finally, ... | In this example, we use the native array iterator of the `data` object to make our `SpecialList` iterable, returning the exact values of the `data` array. Meanwhile, we also define a `values` method, which is an iterator itself, using `Array.prototype.filter()` and `Array.prototype.map()` on the `data` array. Finally, ... | code_snippets | ||
b439f7f4-02ed-4e6d-8421-7fb6bd9f7152 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterators.md | unknown | 55ce89c8-92d0-4d08-871a-dafdecebd9b7 | 1 | SemanticChunker@1.0.0 | 867e20e417068f87339b896a38961401a2fd37b0cf134c3a8b080bc5efd62101 | In the above example, we implement a [`LinkedList` data structure](/js/s/data-structures-linked-list), that internally uses a `data` array. Each item in it has a `value` and some implementation-specific properties used to determine its position in the sequence. Objects constructed from this class are not iterable by de... | unknown | unknown | In the above example, we implement a [`LinkedList` data structure](/js/s/data-structures-linked-list), that internally uses a `data` array. Each item in it has a `value` and some implementation-specific properties used to determine its position in the sequence. Objects constructed from this class are not iterable by de... | In the above example, we implement a [`LinkedList` data structure](/js/s/data-structures-linked-list), that internally uses a `data` array. Each item in it has a `value` and some implementation-specific properties used to determine its position in the sequence. Objects constructed from this class are not iterable by de... | code_snippets | ||
c6154beb-cf3f-47db-ab34-7611a50923aa | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterators.md | unknown | 55ce89c8-92d0-4d08-871a-dafdecebd9b7 | 0 | SemanticChunker@1.0.0 | f10e439df2643ac642500d80594254eb427945284ec6145ac5d9c94b2a176969 | ---
title: What are JavaScript Iterators and where can I use them?
shortTitle: JavaScript iterators introduction
language: javascript
tags: [array,object,iterator]
cover: balloons
excerpt: Learn how JavaScript's iterators work and how you can use them to level up your projects by understanding these short code examples... | unknown | unknown | ---
title: What are JavaScript Iterators and where can I use them?
shortTitle: JavaScript iterators introduction
language: javascript
tags: [array,object,iterator]
cover: balloons
excerpt: Learn how JavaScript's iterators work and how you can use them to level up your projects by understanding these short code examples... | ---
title: What are JavaScript Iterators and where can I use them?
shortTitle: JavaScript iterators introduction
language: javascript
tags: [array,object,iterator]
cover: balloons
excerpt: Learn how JavaScript's iterators work and how you can use them to level up your projects by understanding these short code examples... | code_snippets | ||
31b65b8b-9680-42a7-8806-01ad902e8ccc | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/json-to-file.md | unknown | efa84779-24b7-4e32-91d3-1709414a377b | 0 | SemanticChunker@1.0.0 | 9148fdfb64e183d61462aba497d06dcf3a77870ea5f54fc9c001156d6e25d3fa | ---
title: How can I save a JSON object to a file using JavaScript?
shortTitle: JSON to file
language: javascript
tags: [node, browser]
cover: travel-mug-3
excerpt: Learn how to write a JSON object to a file, both using Node.js and in the browser.
listed: true
dateModified: 2024-02-01
---
JSON objects are a common way... | unknown | unknown | ---
title: How can I save a JSON object to a file using JavaScript?
shortTitle: JSON to file
language: javascript
tags: [node, browser]
cover: travel-mug-3
excerpt: Learn how to write a JSON object to a file, both using Node.js and in the browser.
listed: true
dateModified: 2024-02-01
---
JSON objects are a common way... | ---
title: How can I save a JSON object to a file using JavaScript?
shortTitle: JSON to file
language: javascript
tags: [node, browser]
cover: travel-mug-3
excerpt: Learn how to write a JSON object to a file, both using Node.js and in the browser.
listed: true
dateModified: 2024-02-01
---
JSON objects are a common way... | code_snippets | ||
65b398f5-9440-46fb-8e4f-06f2b942ab86 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/json-to-file.md | unknown | efa84779-24b7-4e32-91d3-1709414a377b | 1 | SemanticChunker@1.0.0 | 0a26a919b655f68b61c1daf1f97cabece84ca2d2f7dc275a47db39be87d7df93 | [Save a JSON object to a file in Node.js]
## Save a JSON object to a file in Node.js
Node.js provides a **built-in module** called `fs` that allows you to work with the file system. You can use the `fs.writeFileSync()` method to write a JSON object to a file. To convert the JSON object to a string, you can use `JSON.... | unknown | unknown | [Save a JSON object to a file in Node.js]
## Save a JSON object to a file in Node.js
Node.js provides a **built-in module** called `fs` that allows you to work with the file system. You can use the `fs.writeFileSync()` method to write a JSON object to a file. To convert the JSON object to a string, you can use `JSON.... | [Save a JSON object to a file in Node.js]
## Save a JSON object to a file in Node.js
Node.js provides a **built-in module** called `fs` that allows you to work with the file system. You can use the `fs.writeFileSync()` method to write a JSON object to a file. To convert the JSON object to a string, you can use `JSON.... | code_snippets | ||
7c7d2ea5-12e1-443b-a176-d87d67856f39 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/json-to-file.md | unknown | efa84779-24b7-4e32-91d3-1709414a377b | 2 | SemanticChunker@1.0.0 | 50ee6b336875bc34edc3326b46e582118d478267c0787a27a406afc8aa81b2c7 | [Save a JSON object to a file in Node.js > Save a JSON object to a file in the browser]
## Save a JSON object to a file in the browser
In the browser, you can use the `Blob` and `URL.createObjectURL()` to create a **downloadable file**. You can the use `Document.createElement()` to create an **anchor element** (`a`),... | unknown | unknown | [Save a JSON object to a file in Node.js > Save a JSON object to a file in the browser]
## Save a JSON object to a file in the browser
In the browser, you can use the `Blob` and `URL.createObjectURL()` to create a **downloadable file**. You can the use `Document.createElement()` to create an **anchor element** (`a`),... | [Save a JSON object to a file in Node.js > Save a JSON object to a file in the browser]
## Save a JSON object to a file in the browser
In the browser, you can use the `Blob` and `URL.createObjectURL()` to create a **downloadable file**. You can the use `Document.createElement()` to create an **anchor element** (`a`),... | code_snippets | ||
0f4db408-03f3-4ddd-b1a6-517c08dfb98e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterable-to-array.md | unknown | 517adca9-3690-41b5-9339-89a9c1696b89 | 2 | SemanticChunker@1.0.0 | 30ff2fbc820b2caf112397f2686f74e388d3cd3a10f274d90b0f34082c0ff92d | [String > Set]
## Set
A [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) is a collection of unique values. When the spread operator is applied to it, the result is an array of the stored values:
```js
const data = [1, 2, 3, 1, 2, 4]
const values = new Set(data);
const uni... | unknown | unknown | [String > Set]
## Set
A [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) is a collection of unique values. When the spread operator is applied to it, the result is an array of the stored values:
```js
const data = [1, 2, 3, 1, 2, 4]
const values = new Set(data);
const uni... | [String > Set]
## Set
A [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) is a collection of unique values. When the spread operator is applied to it, the result is an array of the stored values:
```js
const data = [1, 2, 3, 1, 2, 4]
const values = new Set(data);
const uni... | code_snippets | ||
3ec2e52f-ef30-4bd0-b99b-12a018cfd714 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterable-to-array.md | unknown | 517adca9-3690-41b5-9339-89a9c1696b89 | 0 | SemanticChunker@1.0.0 | ef0516f91fb8efda8e236169755166f0683e5afd83467755bbadf0ffd2a5a5a2 | ---
title: How do I convert an iterable to an array in JavaScript?
shortTitle: Iterable to array
language: javascript
tags: [object,array,string]
cover: waves
excerpt: Learn how to use the JavaScript ES6 spread syntax to converting iterables to arrays and level up your code today.
listed: true
dateModified: 2021-06-12
... | unknown | unknown | ---
title: How do I convert an iterable to an array in JavaScript?
shortTitle: Iterable to array
language: javascript
tags: [object,array,string]
cover: waves
excerpt: Learn how to use the JavaScript ES6 spread syntax to converting iterables to arrays and level up your code today.
listed: true
dateModified: 2021-06-12
... | ---
title: How do I convert an iterable to an array in JavaScript?
shortTitle: Iterable to array
language: javascript
tags: [object,array,string]
cover: waves
excerpt: Learn how to use the JavaScript ES6 spread syntax to converting iterables to arrays and level up your code today.
listed: true
dateModified: 2021-06-12
... | code_snippets | ||
61a222ce-878b-4c76-b54e-9f32daeabda9 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterable-to-array.md | unknown | 517adca9-3690-41b5-9339-89a9c1696b89 | 1 | SemanticChunker@1.0.0 | 0b05248248f77a97b12b678a6b0989f2ee5a3e32fa596c6ec379f763f8211a4b | [String]
## String
When the spread operator is applied to a string, the result is an array of strings each one representing a character of the original string:
```js
const name = 'Zelda';
const letters = [...name]; // 'Z', 'e', 'l', 'd', 'a'
``` | unknown | unknown | [String]
## String
When the spread operator is applied to a string, the result is an array of strings each one representing a character of the original string:
```js
const name = 'Zelda';
const letters = [...name]; // 'Z', 'e', 'l', 'd', 'a'
``` | [String]
## String
When the spread operator is applied to a string, the result is an array of strings each one representing a character of the original string:
```js
const name = 'Zelda';
const letters = [...name]; // 'Z', 'e', 'l', 'd', 'a'
``` | code_snippets | ||
d220a6cb-c527-4374-b95b-08ed7a0affe5 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/iterable-to-array.md | unknown | 517adca9-3690-41b5-9339-89a9c1696b89 | 3 | SemanticChunker@1.0.0 | 88d8e83b22cad55c8645e7ad6ad2b6f90c907c80ef40bbadf8676f14c14dcac2 | [String > NodeList]
## NodeList
A [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) is a collection of nodes, returned by methods such as `Document.childNodes()` or `Document.querySelectorAll()`. While it implements some methods that help manipulate it as an array (e.g. `NodeList.prototype.forEach... | unknown | unknown | [String > NodeList]
## NodeList
A [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) is a collection of nodes, returned by methods such as `Document.childNodes()` or `Document.querySelectorAll()`. While it implements some methods that help manipulate it as an array (e.g. `NodeList.prototype.forEach... | [String > NodeList]
## NodeList
A [NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) is a collection of nodes, returned by methods such as `Document.childNodes()` or `Document.querySelectorAll()`. While it implements some methods that help manipulate it as an array (e.g. `NodeList.prototype.forEach... | code_snippets | ||
713444a1-68d0-4c90-8e0b-6beabe01e4b3 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/is-same-origin.md | unknown | 0a799322-18c5-4c63-8238-63b6e04ab8bb | 0 | SemanticChunker@1.0.0 | cf7e22341e4f6b5ca0df90c73f92ddbe3f5ff0ef026be2fa0ca19ddd1ea432da | ---
title: Check if two URLs are on the same origin
shortTitle: Same origin URL check
language: javascript
tags: [browser]
cover: leafy-screens
excerpt: Can you tell if two URLs are on the same origin? Here's a very simple way to do so.
listed: true
dateModified: 2024-07-08
---
Two URLs are considered to be on the **s... | unknown | unknown | ---
title: Check if two URLs are on the same origin
shortTitle: Same origin URL check
language: javascript
tags: [browser]
cover: leafy-screens
excerpt: Can you tell if two URLs are on the same origin? Here's a very simple way to do so.
listed: true
dateModified: 2024-07-08
---
Two URLs are considered to be on the **s... | ---
title: Check if two URLs are on the same origin
shortTitle: Same origin URL check
language: javascript
tags: [browser]
cover: leafy-screens
excerpt: Can you tell if two URLs are on the same origin? Here's a very simple way to do so.
listed: true
dateModified: 2024-07-08
---
Two URLs are considered to be on the **s... | code_snippets | ||
266e4f12-3d54-4750-8514-bbd14f30e01e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 2 | SemanticChunker@1.0.0 | 908241ba6ef92711adecae591b8128ba097c1822119a61ef945bfa670a30bc0b | [Problem statement > Solution visualization]
### Solution visualization
Here's a step-by-step guide to finding the LCS of these two strings using this approach. Use the buttons below the table to **replay each step** and move forward or backward.
<step-visualizer>
| | ε | G | X | T | X | A | Y | B |
| ----- | - | -... | unknown | unknown | [Problem statement > Solution visualization]
### Solution visualization
Here's a step-by-step guide to finding the LCS of these two strings using this approach. Use the buttons below the table to **replay each step** and move forward or backward.
<step-visualizer>
| | ε | G | X | T | X | A | Y | B |
| ----- | - | -... | [Problem statement > Solution visualization]
### Solution visualization
Here's a step-by-step guide to finding the LCS of these two strings using this approach. Use the buttons below the table to **replay each step** and move forward or backward.
<step-visualizer>
| | ε | G | X | T | X | A | Y | B |
| ----- | - | -... | code_snippets | ||
3332a6ce-d947-471d-ae4f-576747558e1f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 5 | SemanticChunker@1.0.0 | 3e457121fae42e809c0ce0b513c123e7aabf1ba26daed274004ef28d0b4e0ea5 | [Solution explanation > Short explanation]
### Short explanation
Here are the algorithm's steps, given two sequences, `a` and `b`, of lengths `m` and `n`:
1. Create a `(m + 1) x (n + 1)` matrix, `dp`. Each column represents a character of `a`, and each row represents a character of `b`.
2. Fill the first row and col... | unknown | unknown | [Solution explanation > Short explanation]
### Short explanation
Here are the algorithm's steps, given two sequences, `a` and `b`, of lengths `m` and `n`:
1. Create a `(m + 1) x (n + 1)` matrix, `dp`. Each column represents a character of `a`, and each row represents a character of `b`.
2. Fill the first row and col... | [Solution explanation > Short explanation]
### Short explanation
Here are the algorithm's steps, given two sequences, `a` and `b`, of lengths `m` and `n`:
1. Create a `(m + 1) x (n + 1)` matrix, `dp`. Each column represents a character of `a`, and each row represents a character of `b`.
2. Fill the first row and col... | code_snippets | ||
41364974-af46-4009-be7d-1fcf62b0d491 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 7 | SemanticChunker@1.0.0 | 6c028510891c3f7acb0d38353734cb7977f894d4df6c97fd777d68e2ae68d3b6 | [Problem statement > Algorithm implementation]
## Algorithm implementation
Now that you better understand the problem and how to solve it, let's implement the algorithm in JavaScript. And, to spice things up a little, we'll make sure it can handle **both strings and arrays**.
```js
const longestCommonSubsequence = (... | unknown | unknown | [Problem statement > Algorithm implementation]
## Algorithm implementation
Now that you better understand the problem and how to solve it, let's implement the algorithm in JavaScript. And, to spice things up a little, we'll make sure it can handle **both strings and arrays**.
```js
const longestCommonSubsequence = (... | [Problem statement > Algorithm implementation]
## Algorithm implementation
Now that you better understand the problem and how to solve it, let's implement the algorithm in JavaScript. And, to spice things up a little, we'll make sure it can handle **both strings and arrays**.
```js
const longestCommonSubsequence = (... | code_snippets | ||
69c5f5ca-a3a1-459a-9ce1-1f080b8e3dc0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 1 | SemanticChunker@1.0.0 | 486908044a649669e313a1aac5183a5bd0bc56969f9790231421ae268152a7bc | [Problem statement]
## Problem statement
Before we jump into the code, let's look at an example to understand the problem better. Consider the following two strings:
```js
const string1 = 'AGGTAB';
const string2 = 'GXTXAYB';
```
The longest common subsequence of these two strings is `'GTAB'`, which has a length of ... | unknown | unknown | [Problem statement]
## Problem statement
Before we jump into the code, let's look at an example to understand the problem better. Consider the following two strings:
```js
const string1 = 'AGGTAB';
const string2 = 'GXTXAYB';
```
The longest common subsequence of these two strings is `'GTAB'`, which has a length of ... | [Problem statement]
## Problem statement
Before we jump into the code, let's look at an example to understand the problem better. Consider the following two strings:
```js
const string1 = 'AGGTAB';
const string2 = 'GXTXAYB';
```
The longest common subsequence of these two strings is `'GTAB'`, which has a length of ... | code_snippets | ||
90345727-9c8d-4f67-a8a4-b4b896d3dc8e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 0 | SemanticChunker@1.0.0 | 583ebc38f2e47574188cf5e16b49277ad947862c3beeb5545a803e2f534ef843 | ---
title: How can I find the longest common subsequence of two strings in JavaScript?
shortTitle: Longest Common Subsequence
language: javascript
tags: [string,array,algorithm]
cover: carrots
excerpt: The longest common subsequence is the longest subsequence common to all given sequences and can be easily found using ... | unknown | unknown | ---
title: How can I find the longest common subsequence of two strings in JavaScript?
shortTitle: Longest Common Subsequence
language: javascript
tags: [string,array,algorithm]
cover: carrots
excerpt: The longest common subsequence is the longest subsequence common to all given sequences and can be easily found using ... | ---
title: How can I find the longest common subsequence of two strings in JavaScript?
shortTitle: Longest Common Subsequence
language: javascript
tags: [string,array,algorithm]
cover: carrots
excerpt: The longest common subsequence is the longest subsequence common to all given sequences and can be easily found using ... | code_snippets | ||
911682d3-d7a6-48fe-b658-27ceba0589fa | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 6 | SemanticChunker@1.0.0 | ea4c68ea49b2850338ee6801eeb5eebe004dc8826f69a57d3ea1b7730409446b | [Solution explanation > Long explanation]
### Long explanation
Like I said earlier, in order to solve the problem, we'll break it into smaller subproblems. The table in the visualization shows the **lengths of the longest common subsequences of the prefixes** of the two strings. The **rows** represent the characters ... | unknown | unknown | [Solution explanation > Long explanation]
### Long explanation
Like I said earlier, in order to solve the problem, we'll break it into smaller subproblems. The table in the visualization shows the **lengths of the longest common subsequences of the prefixes** of the two strings. The **rows** represent the characters ... | [Solution explanation > Long explanation]
### Long explanation
Like I said earlier, in order to solve the problem, we'll break it into smaller subproblems. The table in the visualization shows the **lengths of the longest common subsequences of the prefixes** of the two strings. The **rows** represent the characters ... | code_snippets | ||
c5733724-8ba1-41b7-8501-9857308f06a7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 3 | SemanticChunker@1.0.0 | b2da42bd45b77fd17156420b62f97fa4f5c1377f0c2e2faaa5ab18ce4b12a03a | [Problem statement > Solution visualization]
| | ε | G | X | T | X | A | Y | B |
| ----- | - | - | - | - | - | - | - | - |
| **ε** | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| **A** | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **T** | 0 | 1 | 1 | 2 | 2 ... | unknown | unknown | [Problem statement > Solution visualization]
| | ε | G | X | T | X | A | Y | B |
| ----- | - | - | - | - | - | - | - | - |
| **ε** | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| **A** | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **T** | 0 | 1 | 1 | 2 | 2 ... | [Problem statement > Solution visualization]
| | ε | G | X | T | X | A | Y | B |
| ----- | - | - | - | - | - | - | - | - |
| **ε** | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| **A** | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **G** | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| **T** | 0 | 1 | 1 | 2 | 2 ... | code_snippets | ||
c7a7e49c-c723-42d0-a413-b6e0c0ca4527 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-common-subsequence.md | unknown | 1a6d306d-ccd6-4023-a8d3-1ca245d75b16 | 4 | SemanticChunker@1.0.0 | 48b43bf7731e2a68a9000f7cc7410b87f035ba3282fd353e762412faae35e0a4 | [Problem statement > Solution explanation]
## Solution explanation
Cool visualization, huh? _But how does it work?_ I'll first present the short version of these steps, and then we'll dive into the details. Feel free to skip either one of the two, as long as you understand the concept. | unknown | unknown | [Problem statement > Solution explanation]
## Solution explanation
Cool visualization, huh? _But how does it work?_ I'll first present the short version of these steps, and then we'll dive into the details. Feel free to skip either one of the two, as long as you understand the concept. | [Problem statement > Solution explanation]
## Solution explanation
Cool visualization, huh? _But how does it work?_ I'll first present the short version of these steps, and then we'll dive into the details. Feel free to skip either one of the two, as long as you understand the concept. | code_snippets | ||
2d057b57-5e47-4095-96e5-678ad371172f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/listify.md | unknown | b5f2df65-3e15-42b4-ad4f-00993f2891eb | 0 | SemanticChunker@1.0.0 | 99c28d6ff024c0c480ddff84c04b6c50898226f27e100ae43828fbcd67bb99da | ---
title: Map a JavaScript object to an array
shortTitle: Map object to array
language: javascript
tags: [object,array]
cover: metro-arrival
excerpt: Map an object to an object array, using the provided mapping function.
listed: true
dateModified: 2024-03-22
---
Ever wanted to **map an object to an array of objects**... | unknown | unknown | ---
title: Map a JavaScript object to an array
shortTitle: Map object to array
language: javascript
tags: [object,array]
cover: metro-arrival
excerpt: Map an object to an object array, using the provided mapping function.
listed: true
dateModified: 2024-03-22
---
Ever wanted to **map an object to an array of objects**... | ---
title: Map a JavaScript object to an array
shortTitle: Map object to array
language: javascript
tags: [object,array]
cover: metro-arrival
excerpt: Map an object to an object array, using the provided mapping function.
listed: true
dateModified: 2024-03-22
---
Ever wanted to **map an object to an array of objects**... | code_snippets | ||
64d0fdec-1636-4427-9824-d3b5e3af8f18 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/listify.md | unknown | b5f2df65-3e15-42b4-ad4f-00993f2891eb | 2 | SemanticChunker@1.0.0 | d7f359a5a7d101aff950fd5e24694e92ee85bd78d228113fb44e942cee334db8 | [Convert an object to an array of objects > Convert an array of objects to an object]
## Convert an array of objects to an object
Conversely, what about the opposite? You can simply use `Object.fromEntries()` to **convert an array of objects to an object**. The `mapFn` function should return a **2-element array** wit... | unknown | unknown | [Convert an object to an array of objects > Convert an array of objects to an object]
## Convert an array of objects to an object
Conversely, what about the opposite? You can simply use `Object.fromEntries()` to **convert an array of objects to an object**. The `mapFn` function should return a **2-element array** wit... | [Convert an object to an array of objects > Convert an array of objects to an object]
## Convert an array of objects to an object
Conversely, what about the opposite? You can simply use `Object.fromEntries()` to **convert an array of objects to an object**. The `mapFn` function should return a **2-element array** wit... | code_snippets | ||
a949a153-5a6e-4473-9599-3f88b1e34276 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/listify.md | unknown | b5f2df65-3e15-42b4-ad4f-00993f2891eb | 1 | SemanticChunker@1.0.0 | db8055ae4742175733f0f545df32585edf3bfc5639b7df7dec41b8a23a9d8353 | [Convert an object to an array of objects]
## Convert an object to an array of objects
The simplest way to get the **key-value pairs** of an object is to use `Object.entries()`. Then, using `Array.prototype.map()` and a **mapping function**, you can map the key-value pairs to an array of objects.
```js
const listify... | unknown | unknown | [Convert an object to an array of objects]
## Convert an object to an array of objects
The simplest way to get the **key-value pairs** of an object is to use `Object.entries()`. Then, using `Array.prototype.map()` and a **mapping function**, you can map the key-value pairs to an array of objects.
```js
const listify... | [Convert an object to an array of objects]
## Convert an object to an array of objects
The simplest way to get the **key-value pairs** of an object is to use `Object.entries()`. Then, using `Array.prototype.map()` and a **mapping function**, you can map the key-value pairs to an array of objects.
```js
const listify... | code_snippets | ||
0f2cfb36-bd26-4a32-8274-603537ed8ee5 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 2 | SemanticChunker@1.0.0 | 71e33db5b14b739c8c4c31cefc27233c976b8b8846745824e0d4d43cd649571d | [Logical AND > Logical OR]
## Logical OR
The **logical or** (`||`) operator returns `true` if at least one of the operands is `true`, otherwise it returns `false`.
```js
const or = (a, b) => a || b;
or(true, true); // true
or(true, false); // true
or(false, false); // false
```
| a | b | a \|\| b |
| ----- | -----... | unknown | unknown | [Logical AND > Logical OR]
## Logical OR
The **logical or** (`||`) operator returns `true` if at least one of the operands is `true`, otherwise it returns `false`.
```js
const or = (a, b) => a || b;
or(true, true); // true
or(true, false); // true
or(false, false); // false
```
| a | b | a \|\| b |
| ----- | -----... | [Logical AND > Logical OR]
## Logical OR
The **logical or** (`||`) operator returns `true` if at least one of the operands is `true`, otherwise it returns `false`.
```js
const or = (a, b) => a || b;
or(true, true); // true
or(true, false); // true
or(false, false); // false
```
| a | b | a \|\| b |
| ----- | -----... | code_snippets | ||
20a02eb6-1dbd-4c9d-8dc0-1d6eafc1d208 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 0 | SemanticChunker@1.0.0 | dd91af53393f4b77fea64adb501244b324d72ad6ebcb6bd27a76d636d5059dc6 | ---
title: JavaScript Logical Operations
shortTitle: Logical Operations
language: javascript
tags: [math,logic,function]
cover: chess-pawns
excerpt: Get started with logical operations in JavaScript with this collection of helper functions.
listed: true
dateModified: 2023-05-07
---
Boolean logic and **logical operatio... | unknown | unknown | ---
title: JavaScript Logical Operations
shortTitle: Logical Operations
language: javascript
tags: [math,logic,function]
cover: chess-pawns
excerpt: Get started with logical operations in JavaScript with this collection of helper functions.
listed: true
dateModified: 2023-05-07
---
Boolean logic and **logical operatio... | ---
title: JavaScript Logical Operations
shortTitle: Logical Operations
language: javascript
tags: [math,logic,function]
cover: chess-pawns
excerpt: Get started with logical operations in JavaScript with this collection of helper functions.
listed: true
dateModified: 2023-05-07
---
Boolean logic and **logical operatio... | code_snippets | ||
2ca1f870-f1ab-4bd0-bcfa-d79d8aeb91c3 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 5 | SemanticChunker@1.0.0 | 579db1eb68bdb9310476ec17949f48e77fdce9e9bdcebc12022a13ab58827c01 | [Logical AND > Logical NOR]
## Logical NOR
The **logical nor** operator is a combination of the logical not and logical or operators. It returns `true` if none of the operands are `true`, otherwise it returns `false`.
```js
const nor = (a, b) => !(a||b);
nor(true, true); // false
nor(true, false); // false
nor(fals... | unknown | unknown | [Logical AND > Logical NOR]
## Logical NOR
The **logical nor** operator is a combination of the logical not and logical or operators. It returns `true` if none of the operands are `true`, otherwise it returns `false`.
```js
const nor = (a, b) => !(a||b);
nor(true, true); // false
nor(true, false); // false
nor(fals... | [Logical AND > Logical NOR]
## Logical NOR
The **logical nor** operator is a combination of the logical not and logical or operators. It returns `true` if none of the operands are `true`, otherwise it returns `false`.
```js
const nor = (a, b) => !(a||b);
nor(true, true); // false
nor(true, false); // false
nor(fals... | code_snippets | ||
5c0c111e-a430-465a-ba5c-8ebd3429575a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 4 | SemanticChunker@1.0.0 | b62c2ac8ea2431938d59714bdf3d596b4c2bece4aafddfba6090ed50a51448ec | [Logical AND > Logical XOR]
## Logical XOR
The **logical xor** (`^`) operator returns `true` if exactly one of the operands is `true`, otherwise it returns `false`.
> [!NOTE]
>
> While JavaScript implements the XOR (`^`) operator, it's only for **bitwise operations**. It's not a logical operator, so it doesn't work ... | unknown | unknown | [Logical AND > Logical XOR]
## Logical XOR
The **logical xor** (`^`) operator returns `true` if exactly one of the operands is `true`, otherwise it returns `false`.
> [!NOTE]
>
> While JavaScript implements the XOR (`^`) operator, it's only for **bitwise operations**. It's not a logical operator, so it doesn't work ... | [Logical AND > Logical XOR]
## Logical XOR
The **logical xor** (`^`) operator returns `true` if exactly one of the operands is `true`, otherwise it returns `false`.
> [!NOTE]
>
> While JavaScript implements the XOR (`^`) operator, it's only for **bitwise operations**. It's not a logical operator, so it doesn't work ... | code_snippets | ||
760bef2f-6bb6-4f87-9bcd-c43ce310d17c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 6 | SemanticChunker@1.0.0 | a0d5cd9b6a0d139814fc82e58d93341d7c38256df653242c3056b83504d9f2c5 | [Logical AND > Logical NAND]
## Logical NAND
The **logical nand** operator is a combination of the logical not and logical and operators. It returns `true` if at least one of the operands is `false`, otherwise it returns `false`.
```js
const nand = (a, b) => !(a&&b);
nand(true, true); // false
nand(true, false); //... | unknown | unknown | [Logical AND > Logical NAND]
## Logical NAND
The **logical nand** operator is a combination of the logical not and logical and operators. It returns `true` if at least one of the operands is `false`, otherwise it returns `false`.
```js
const nand = (a, b) => !(a&&b);
nand(true, true); // false
nand(true, false); //... | [Logical AND > Logical NAND]
## Logical NAND
The **logical nand** operator is a combination of the logical not and logical and operators. It returns `true` if at least one of the operands is `false`, otherwise it returns `false`.
```js
const nand = (a, b) => !(a&&b);
nand(true, true); // false
nand(true, false); //... | code_snippets | ||
769e5a5f-e992-4975-ac8d-63bdb8116c27 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 3 | SemanticChunker@1.0.0 | eb90bef01e462588c1b8ba6884ae83d3891c27005e80cc13a103e5f847544599 | [Logical AND > Logical NOT]
## Logical NOT
The **logical not** (`!`) operator returns `true` if the operand is `false`, otherwise it returns `false`.
```js
const not = a => !a;
not(true); // false
not(false); // true
```
| a | !a |
| ----- | ----- |
| true | false |
| false | true | | unknown | unknown | [Logical AND > Logical NOT]
## Logical NOT
The **logical not** (`!`) operator returns `true` if the operand is `false`, otherwise it returns `false`.
```js
const not = a => !a;
not(true); // false
not(false); // true
```
| a | !a |
| ----- | ----- |
| true | false |
| false | true | | [Logical AND > Logical NOT]
## Logical NOT
The **logical not** (`!`) operator returns `true` if the operand is `false`, otherwise it returns `false`.
```js
const not = a => !a;
not(true); // false
not(false); // true
```
| a | !a |
| ----- | ----- |
| true | false |
| false | true | | code_snippets | ||
81790ab1-5b27-432b-af5e-4272e2be2335 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 1 | SemanticChunker@1.0.0 | 1ffc1d371e7f9a0f36016c6fe1052001c9ff8292e089ffe26f83231a9b4fab37 | [Logical AND]
## Logical AND
The **logical and** (`&&`) operator returns `true` if both operands are `true`, otherwise it returns `false`.
```js
const and = (a, b) => a && b;
and(true, true); // true
and(true, false); // false
and(false, false); // false
```
| a | b | a && b |
| ----- | ----- | ------ |
| true | t... | unknown | unknown | [Logical AND]
## Logical AND
The **logical and** (`&&`) operator returns `true` if both operands are `true`, otherwise it returns `false`.
```js
const and = (a, b) => a && b;
and(true, true); // true
and(true, false); // false
and(false, false); // false
```
| a | b | a && b |
| ----- | ----- | ------ |
| true | t... | [Logical AND]
## Logical AND
The **logical and** (`&&`) operator returns `true` if both operands are `true`, otherwise it returns `false`.
```js
const and = (a, b) => a && b;
and(true, true); // true
and(true, false); // false
and(false, false); // false
```
| a | b | a && b |
| ----- | ----- | ------ |
| true | t... | code_snippets | ||
b9dfca64-ba58-46a2-b6ac-af3c84baf60d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/logical-operations.md | unknown | 7e038394-6801-4ec0-80eb-d2d3545eac03 | 7 | SemanticChunker@1.0.0 | fa4224a02c49dd0dc27bdd5bc563a27177455934a196e66377e5b067fec6a57b | [Logical AND > Implementing logical operations with functions]
## Implementing logical operations with functions
The logical operations we've seen so far work with **boolean values**, but what if we want to use them with **functions**? For example, what if we want to check if both of two functions return `true` for a... | unknown | unknown | [Logical AND > Implementing logical operations with functions]
## Implementing logical operations with functions
The logical operations we've seen so far work with **boolean values**, but what if we want to use them with **functions**? For example, what if we want to check if both of two functions return `true` for a... | [Logical AND > Implementing logical operations with functions]
## Implementing logical operations with functions
The logical operations we've seen so far work with **boolean values**, but what if we want to use them with **functions**? For example, what if we want to check if both of two functions return `true` for a... | code_snippets | ||
e26b7920-9499-4626-ac1c-ef5bb5320ead | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-array-element.md | unknown | 8d803d38-9852-4761-9913-5f9bfe4d4130 | 0 | SemanticChunker@1.0.0 | ebe78fa164c76354aac14c511b58cc2c241fe0bdbdb1a5ad4ebfd42d1303adb9 | ---
title: Find the longest element in a JavaScript array
shortTitle: Longest array element
language: javascript
tags: [array]
cover: interior-14
excerpt: Find the element with the greatest `length` in a JavaScript array.
listed: true
dateModified: 2024-08-04
---
I've often found a need to **find the longest element**... | unknown | unknown | ---
title: Find the longest element in a JavaScript array
shortTitle: Longest array element
language: javascript
tags: [array]
cover: interior-14
excerpt: Find the element with the greatest `length` in a JavaScript array.
listed: true
dateModified: 2024-08-04
---
I've often found a need to **find the longest element**... | ---
title: Find the longest element in a JavaScript array
shortTitle: Longest array element
language: javascript
tags: [array]
cover: interior-14
excerpt: Find the element with the greatest `length` in a JavaScript array.
listed: true
dateModified: 2024-08-04
---
I've often found a need to **find the longest element**... | code_snippets | ||
14fb64da-2fd8-4b25-8bee-8a6ad55ba9c4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/last-element-of-array.md | unknown | 5e38e503-869e-4754-9b89-d0048a9e0402 | 0 | SemanticChunker@1.0.0 | b51cdee01305e214806a14c7601d1ddea1db8e53474745a31fb4c12192f59896 | ---
title: Get the last element of a JavaScript array
shortTitle: Last element of array
language: javascript
tags: [array]
cover: purple-laptop
excerpt: Array destructuring can be leveraged in many different ways. Here's one of them.
listed: true
dateModified: 2022-08-28
---
If you have worked with JavaScript arrays b... | unknown | unknown | ---
title: Get the last element of a JavaScript array
shortTitle: Last element of array
language: javascript
tags: [array]
cover: purple-laptop
excerpt: Array destructuring can be leveraged in many different ways. Here's one of them.
listed: true
dateModified: 2022-08-28
---
If you have worked with JavaScript arrays b... | ---
title: Get the last element of a JavaScript array
shortTitle: Last element of array
language: javascript
tags: [array]
cover: purple-laptop
excerpt: Array destructuring can be leveraged in many different ways. Here's one of them.
listed: true
dateModified: 2022-08-28
---
If you have worked with JavaScript arrays b... | code_snippets | ||
075c9c6f-35f3-484d-8926-e29dba6a27db | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/levenshtein-distance.md | unknown | 05d062b8-52c3-4390-9c05-c3fdcaad9894 | 2 | SemanticChunker@1.0.0 | d7dfa5e3ef193b138f58d9d96f772cac366d3cbd077efa58a851347f93f53127 | [Definition > Implementation]
## Implementation
The following implementation is based on the [Wagner–Fischer algorithm](https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm). It uses a 2D array to store the distances between all prefixes of the two strings. The last element of the last row of the array cont... | unknown | unknown | [Definition > Implementation]
## Implementation
The following implementation is based on the [Wagner–Fischer algorithm](https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm). It uses a 2D array to store the distances between all prefixes of the two strings. The last element of the last row of the array cont... | [Definition > Implementation]
## Implementation
The following implementation is based on the [Wagner–Fischer algorithm](https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm). It uses a 2D array to store the distances between all prefixes of the two strings. The last element of the last row of the array cont... | code_snippets | ||
737f6ebc-4fdd-4a7e-8977-95425b4ba13a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/levenshtein-distance.md | unknown | 05d062b8-52c3-4390-9c05-c3fdcaad9894 | 3 | SemanticChunker@1.0.0 | 9b4af0f0dc4a4b8dec3bb457c6fa57cda7827b1b5d969e7f4a94f0add10262c5 | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(mn)`, where `m` and `n` are the lengths of the two strings. The **space complexity** is `O(mn)` as well, since we are creating a 2D array of that size.
A possible **modification** to reduce the space complexity to `O(min(m, n))` ... | unknown | unknown | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(mn)`, where `m` and `n` are the lengths of the two strings. The **space complexity** is `O(mn)` as well, since we are creating a 2D array of that size.
A possible **modification** to reduce the space complexity to `O(min(m, n))` ... | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(mn)`, where `m` and `n` are the lengths of the two strings. The **space complexity** is `O(mn)` as well, since we are creating a 2D array of that size.
A possible **modification** to reduce the space complexity to `O(min(m, n))` ... | code_snippets | ||
9d90d3f1-b4e5-4de2-960c-d7aaad552596 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/levenshtein-distance.md | unknown | 05d062b8-52c3-4390-9c05-c3fdcaad9894 | 1 | SemanticChunker@1.0.0 | ea78d76fece52201a2b5067bc6bfcb48b359ca41304ce62be3173f75ab68730d | [Definition]
## Definition
The [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) is a **measure of the difference between two strings**. It is defined as the **minimum number of single-character edits** (insertions, deletions or substitutions) required to change one string into the other. It ... | unknown | unknown | [Definition]
## Definition
The [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) is a **measure of the difference between two strings**. It is defined as the **minimum number of single-character edits** (insertions, deletions or substitutions) required to change one string into the other. It ... | [Definition]
## Definition
The [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) is a **measure of the difference between two strings**. It is defined as the **minimum number of single-character edits** (insertions, deletions or substitutions) required to change one string into the other. It ... | code_snippets | ||
ecd36d62-9edd-44cc-9195-81e4cd7fc5d4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/levenshtein-distance.md | unknown | 05d062b8-52c3-4390-9c05-c3fdcaad9894 | 0 | SemanticChunker@1.0.0 | 12d861c074857f831bd63849807e31c9dd4ef00e5a6e20feb93a955dd63808a0 | ---
title: Calculate the Levenshtein distance between two strings in JavaScript
shortTitle: Levenshtein distance
language: javascript
tags: [string,algorithm]
cover: purple-sunset-waves
excerpt: Implement the Levenshtein distance algorithm in JavaScript to calculate the difference between two strings.
listed: true
date... | unknown | unknown | ---
title: Calculate the Levenshtein distance between two strings in JavaScript
shortTitle: Levenshtein distance
language: javascript
tags: [string,algorithm]
cover: purple-sunset-waves
excerpt: Implement the Levenshtein distance algorithm in JavaScript to calculate the difference between two strings.
listed: true
date... | ---
title: Calculate the Levenshtein distance between two strings in JavaScript
shortTitle: Levenshtein distance
language: javascript
tags: [string,algorithm]
cover: purple-sunset-waves
excerpt: Implement the Levenshtein distance algorithm in JavaScript to calculate the difference between two strings.
listed: true
date... | code_snippets | ||
bab94e13-72a6-44a0-beb1-7cacf5c40d38 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/listen-click-outside-event.md | unknown | d6b7e51b-e17c-470f-8606-243098fe16fa | 0 | SemanticChunker@1.0.0 | f362e30f033498371474fd844243211e916c94b539adbcd06251e6a322ca2ce9 | ---
title: How can I listen for a click outside of an element in JavaScript?
shortTitle: Listen for click outside element
language: javascript
tags: [browser,event]
cover: interior-13
excerpt: Detect and handle click events outside of a specific element in just a few lines of JavaScript.
listed: true
dateModified: 2023... | unknown | unknown | ---
title: How can I listen for a click outside of an element in JavaScript?
shortTitle: Listen for click outside element
language: javascript
tags: [browser,event]
cover: interior-13
excerpt: Detect and handle click events outside of a specific element in just a few lines of JavaScript.
listed: true
dateModified: 2023... | ---
title: How can I listen for a click outside of an element in JavaScript?
shortTitle: Listen for click outside element
language: javascript
tags: [browser,event]
cover: interior-13
excerpt: Detect and handle click events outside of a specific element in just a few lines of JavaScript.
listed: true
dateModified: 2023... | code_snippets | ||
1c9f3b46-a37b-4c34-baaa-624d31c1adbf | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/linear-search.md | unknown | 720719fc-5ea5-4ac6-89cf-5859b90842f6 | 0 | SemanticChunker@1.0.0 | 0882bff15a76d252c2f7e2bfb671a06e3560e3f09a2be8ec36201301d273b4d0 | ---
title: Linear search in a JavaScript array
shortTitle: Linear search
language: javascript
tags: [algorithm,array]
cover: tranquil-desktop
excerpt: Use the linear search algorithm to find the first index of a given element in an array.
listed: true
dateModified: 2024-07-19
---
The [linear search algorithm](https://... | unknown | unknown | ---
title: Linear search in a JavaScript array
shortTitle: Linear search
language: javascript
tags: [algorithm,array]
cover: tranquil-desktop
excerpt: Use the linear search algorithm to find the first index of a given element in an array.
listed: true
dateModified: 2024-07-19
---
The [linear search algorithm](https://... | ---
title: Linear search in a JavaScript array
shortTitle: Linear search
language: javascript
tags: [algorithm,array]
cover: tranquil-desktop
excerpt: Use the linear search algorithm to find the first index of a given element in an array.
listed: true
dateModified: 2024-07-19
---
The [linear search algorithm](https://... | code_snippets | ||
2196cb60-f76b-4fcf-92b8-e1d46075ae06 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-nearest-neighbors.md | unknown | 09d80be0-4107-40b6-9bf7-1b2873ca77ab | 1 | SemanticChunker@1.0.0 | 903ae8b1a9d20961eb32b2eb8fa7dac29a62426a62c7d1e1b86f68854d6c7226 | [Definition]
## Definition
The [K-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) algorithm is a **simple, instance-based learning algorithm** used for classification and regression tasks. It classifies a data point based on the majority class of its `k` nearest neighbors in a feature ... | unknown | unknown | [Definition]
## Definition
The [K-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) algorithm is a **simple, instance-based learning algorithm** used for classification and regression tasks. It classifies a data point based on the majority class of its `k` nearest neighbors in a feature ... | [Definition]
## Definition
The [K-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) algorithm is a **simple, instance-based learning algorithm** used for classification and regression tasks. It classifies a data point based on the majority class of its `k` nearest neighbors in a feature ... | code_snippets | ||
3c33ba39-71f1-4a94-b571-718c21f23c75 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-nearest-neighbors.md | unknown | 09d80be0-4107-40b6-9bf7-1b2873ca77ab | 3 | SemanticChunker@1.0.0 | d70ae96ba7276460115cef073c3f89c145c4ba8dc4e0144e042bdfb321000e50 | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(n)` for each classification, where `n` is the number of data points. The **space complexity** is `O(n)` as well, since we are storing the distances and labels for each data point. The algorithm is **non-parametric**, meaning it do... | unknown | unknown | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(n)` for each classification, where `n` is the number of data points. The **space complexity** is `O(n)` as well, since we are storing the distances and labels for each data point. The algorithm is **non-parametric**, meaning it do... | [Definition > Complexity]
## Complexity
The **time complexity** of this algorithm is `O(n)` for each classification, where `n` is the number of data points. The **space complexity** is `O(n)` as well, since we are storing the distances and labels for each data point. The algorithm is **non-parametric**, meaning it do... | code_snippets | ||
8d6d1102-d369-4cb7-8422-d2df8437b987 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-nearest-neighbors.md | unknown | 09d80be0-4107-40b6-9bf7-1b2873ca77ab | 0 | SemanticChunker@1.0.0 | 8e740fe8b3d9278d83d8896bce918d55a2c643e38f1acc62e21e178e0cd8628d | ---
title: Classify data using the K-nearest neighbors algorithm in JavaScript
shortTitle: K-nearest neighbors
language: javascript
tags: [algorithm,array]
cover: building-blocks
excerpt: Implement the K-nearest neighbors algorithm in JavaScript to classify a data point relative to a labelled data set.
listed: true
dat... | unknown | unknown | ---
title: Classify data using the K-nearest neighbors algorithm in JavaScript
shortTitle: K-nearest neighbors
language: javascript
tags: [algorithm,array]
cover: building-blocks
excerpt: Implement the K-nearest neighbors algorithm in JavaScript to classify a data point relative to a labelled data set.
listed: true
dat... | ---
title: Classify data using the K-nearest neighbors algorithm in JavaScript
shortTitle: K-nearest neighbors
language: javascript
tags: [algorithm,array]
cover: building-blocks
excerpt: Implement the K-nearest neighbors algorithm in JavaScript to classify a data point relative to a labelled data set.
listed: true
dat... | code_snippets | ||
9e85abd9-1493-4c05-b97d-3483e26e2c7d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-nearest-neighbors.md | unknown | 09d80be0-4107-40b6-9bf7-1b2873ca77ab | 2 | SemanticChunker@1.0.0 | 1d00d72951f479c2dedc66e388b4a1f3522555b7f2e0ed6b1dd7cccd8fb116c5 | [Definition > Implementation]
## Implementation
This implementation classifies a data point relative to a labelled data set, using the K-nearest neighbors algorithm. The `data` is expected to be an array of objects, where each object represents a data point with its features. The `labels` are an array of the correspo... | unknown | unknown | [Definition > Implementation]
## Implementation
This implementation classifies a data point relative to a labelled data set, using the K-nearest neighbors algorithm. The `data` is expected to be an array of objects, where each object represents a data point with its features. The `labels` are an array of the correspo... | [Definition > Implementation]
## Implementation
This implementation classifies a data point relative to a labelled data set, using the K-nearest neighbors algorithm. The `data` is expected to be an array of objects, where each object represents a data point with its features. The `labels` are an array of the correspo... | code_snippets | ||
520df049-0792-467f-af4d-7e6ae960f3d6 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-means.md | unknown | 8410abcc-ca97-48bf-80fe-3cc946d32bb3 | 1 | SemanticChunker@1.0.0 | b570fc5765fc21203992990a14d5c6047f478f0d783fc2b6010a2c349ff36956 | [Definition]
## Definition
The [K-means clustering]((https://en.wikipedia.org/wiki/K-means_clustering)) algorithm is a popular **unsupervised machine learning algorithm** used to group a set of data into clusters. It works by **iteratively** assigning data points to the nearest cluster centroid and then recalculating... | unknown | unknown | [Definition]
## Definition
The [K-means clustering]((https://en.wikipedia.org/wiki/K-means_clustering)) algorithm is a popular **unsupervised machine learning algorithm** used to group a set of data into clusters. It works by **iteratively** assigning data points to the nearest cluster centroid and then recalculating... | [Definition]
## Definition
The [K-means clustering]((https://en.wikipedia.org/wiki/K-means_clustering)) algorithm is a popular **unsupervised machine learning algorithm** used to group a set of data into clusters. It works by **iteratively** assigning data points to the nearest cluster centroid and then recalculating... | code_snippets | ||
b13cbad8-1ceb-4330-a65a-06dcca5c2bdd | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-means.md | unknown | 8410abcc-ca97-48bf-80fe-3cc946d32bb3 | 2 | SemanticChunker@1.0.0 | 50eb8cf4cb6d51da3ba8a4609f5a977349019345ad84b8cdf3254329c8fa93f7 | [Definition > Implementation]
## Implementation
This implementation of the K-means clustering algorithm groups the given data into `k` clusters. **No maximum number of iterations** is set, so the algorithm will run until convergence is reached.
1. As no **initial centroids** are provided, start by using the first `k... | unknown | unknown | [Definition > Implementation]
## Implementation
This implementation of the K-means clustering algorithm groups the given data into `k` clusters. **No maximum number of iterations** is set, so the algorithm will run until convergence is reached.
1. As no **initial centroids** are provided, start by using the first `k... | [Definition > Implementation]
## Implementation
This implementation of the K-means clustering algorithm groups the given data into `k` clusters. **No maximum number of iterations** is set, so the algorithm will run until convergence is reached.
1. As no **initial centroids** are provided, start by using the first `k... | code_snippets | ||
b55729b3-580c-4f68-8d50-1bbaeca9b5e9 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-means.md | unknown | 8410abcc-ca97-48bf-80fe-3cc946d32bb3 | 0 | SemanticChunker@1.0.0 | d0201589114dd40559fa4de730d77b4d3c4b650b0c75308707d3ee082700a0a0 | ---
title: Group data using the K-means clustering algorithm in JavaScript
shortTitle: K-means clustering
language: javascript
tags: [algorithm,array]
cover: antelope
excerpt: Implement the K-means clustering algorithm in JavaScript to group data into clusters.
listed: true
dateModified: 2024-05-11
--- | unknown | unknown | ---
title: Group data using the K-means clustering algorithm in JavaScript
shortTitle: K-means clustering
language: javascript
tags: [algorithm,array]
cover: antelope
excerpt: Implement the K-means clustering algorithm in JavaScript to group data into clusters.
listed: true
dateModified: 2024-05-11
--- | ---
title: Group data using the K-means clustering algorithm in JavaScript
shortTitle: K-means clustering
language: javascript
tags: [algorithm,array]
cover: antelope
excerpt: Implement the K-means clustering algorithm in JavaScript to group data into clusters.
listed: true
dateModified: 2024-05-11
--- | code_snippets | ||
cf34d962-52b7-4140-9f75-2260c4704a77 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-means.md | unknown | 8410abcc-ca97-48bf-80fe-3cc946d32bb3 | 4 | SemanticChunker@1.0.0 | 14795d04c11f304ada253d200846506c0dbe13b67d58f33d9a8012f19e086676 | [Definition > Complexity]
## Complexity
The **time complexity** of the K-means clustering algorithm is `O(n * k * d * i)`, where `n` is the number of data points, `k` is the number of clusters, `d` is the number of dimensions in the data, and `i` is the number of iterations until convergence. The **space complexity**... | unknown | unknown | [Definition > Complexity]
## Complexity
The **time complexity** of the K-means clustering algorithm is `O(n * k * d * i)`, where `n` is the number of data points, `k` is the number of clusters, `d` is the number of dimensions in the data, and `i` is the number of iterations until convergence. The **space complexity**... | [Definition > Complexity]
## Complexity
The **time complexity** of the K-means clustering algorithm is `O(n * k * d * i)`, where `n` is the number of data points, `k` is the number of clusters, `d` is the number of dimensions in the data, and `i` is the number of iterations until convergence. The **space complexity**... | code_snippets | ||
f0526226-05cc-4862-91d6-88efdba138ff | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/k-means.md | unknown | 8410abcc-ca97-48bf-80fe-3cc946d32bb3 | 3 | SemanticChunker@1.0.0 | d2ce0e455f14f88eaee24b06024d7651b651be76876543695f63d414e73d708d | [Definition > Implementation]
```js
const kMeans = (data, k = 1) => {
const centroids = data.slice(0, k);
const distances = Array.from({ length: data.length }, () =>
Array.from({ length: k }, () => 0)
);
const classes = Array.from({ length: data.length }, () => -1);
let itr = true;
while (itr) {
itr = false;
... | unknown | unknown | [Definition > Implementation]
```js
const kMeans = (data, k = 1) => {
const centroids = data.slice(0, k);
const distances = Array.from({ length: data.length }, () =>
Array.from({ length: k }, () => 0)
);
const classes = Array.from({ length: data.length }, () => -1);
let itr = true;
while (itr) {
itr = false;
... | [Definition > Implementation]
```js
const kMeans = (data, k = 1) => {
const centroids = data.slice(0, k);
const distances = Array.from({ length: data.length }, () =>
Array.from({ length: k }, () => 0)
);
const classes = Array.from({ length: data.length }, () => -1);
let itr = true;
while (itr) {
itr = false;
... | code_snippets | ||
0532e1a9-5807-405d-ac30-194ce363d12e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 0 | SemanticChunker@1.0.0 | 07fee11773ca9fceecfcab2a2536cbbd229cc3c11d54ede67e5a7bdeb229f53e | ---
title: Check if two JavaScript objects have the same properties
shortTitle: Match object properties
language: javascript
tags: [object]
cover: two-flower-vases
excerpt: Compare two objects to determine if the first one contains equivalent property values to the second one.
listed: true
dateModified: 2024-03-24
---
... | unknown | unknown | ---
title: Check if two JavaScript objects have the same properties
shortTitle: Match object properties
language: javascript
tags: [object]
cover: two-flower-vases
excerpt: Compare two objects to determine if the first one contains equivalent property values to the second one.
listed: true
dateModified: 2024-03-24
---
... | ---
title: Check if two JavaScript objects have the same properties
shortTitle: Match object properties
language: javascript
tags: [object]
cover: two-flower-vases
excerpt: Compare two objects to determine if the first one contains equivalent property values to the second one.
listed: true
dateModified: 2024-03-24
---
... | code_snippets | ||
5103bbad-3c64-4139-9a88-aa63939ac77b | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 3 | SemanticChunker@1.0.0 | 447bddd50d50f47ec24ec2d1d8c78d4736aacad8bab8d531bef1bc4e12bea173 | [Comparison by value > Comparison using a custom function]
### Comparison using a custom function
If, instead, we want to use a **custom function to determine value equality**, we can simply replace the strict equality operator with the provided function. This function will receive the value of the property in both o... | unknown | unknown | [Comparison by value > Comparison using a custom function]
### Comparison using a custom function
If, instead, we want to use a **custom function to determine value equality**, we can simply replace the strict equality operator with the provided function. This function will receive the value of the property in both o... | [Comparison by value > Comparison using a custom function]
### Comparison using a custom function
If, instead, we want to use a **custom function to determine value equality**, we can simply replace the strict equality operator with the provided function. This function will receive the value of the property in both o... | code_snippets | ||
53373e49-43c6-4aef-ad43-21619a23749f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 1 | SemanticChunker@1.0.0 | 91249d33dd20309e6a45b4969adb612d45a47c0e97487cb42ab5b326d64231b9 | [One-sided comparison]
## One-sided comparison
First off, let's create a function that compares two objects to determine if **the first one contains equivalent property values to the second one**. This essentially means checking if the keys of the first object are a **superset** of the keys of the second object, and ... | unknown | unknown | [One-sided comparison]
## One-sided comparison
First off, let's create a function that compares two objects to determine if **the first one contains equivalent property values to the second one**. This essentially means checking if the keys of the first object are a **superset** of the keys of the second object, and ... | [One-sided comparison]
## One-sided comparison
First off, let's create a function that compares two objects to determine if **the first one contains equivalent property values to the second one**. This essentially means checking if the keys of the first object are a **superset** of the keys of the second object, and ... | code_snippets | ||
7c742fa3-255a-4a8b-9f4d-6c65138c927e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 6 | SemanticChunker@1.0.0 | 35c21c4d2ec1604e4c45deb6cbb52ddb9ac89861cf2396b56f6de8f7d1a34569 | [Two-sided comparison > Comparison using a custom function]
### Comparison using a custom function
For custom value comparison, you can use the same approach as before, using a custom function to determine value equality.
```js
const matchesSymmetricWith = (a, b, fn) => {
const keysA = Object.keys(a),
keysB = Obje... | unknown | unknown | [Two-sided comparison > Comparison using a custom function]
### Comparison using a custom function
For custom value comparison, you can use the same approach as before, using a custom function to determine value equality.
```js
const matchesSymmetricWith = (a, b, fn) => {
const keysA = Object.keys(a),
keysB = Obje... | [Two-sided comparison > Comparison using a custom function]
### Comparison using a custom function
For custom value comparison, you can use the same approach as before, using a custom function to determine value equality.
```js
const matchesSymmetricWith = (a, b, fn) => {
const keysA = Object.keys(a),
keysB = Obje... | code_snippets | ||
99f188ce-c763-4e01-8ae8-baee1b1545fa | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 5 | SemanticChunker@1.0.0 | 3617f76a484830a2041b2b303ffa1351ac178922f91d6058786f35ee05f9fb10 | [Two-sided comparison > Comparison by value]
### Comparison by value
For simple value comparison, you can again use the strict equality operator (`===`).
```js
const matchesSymmetric = (a, b) => {
const keysA = Object.keys(a),
keysB = Object.keys(b);
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
... | unknown | unknown | [Two-sided comparison > Comparison by value]
### Comparison by value
For simple value comparison, you can again use the strict equality operator (`===`).
```js
const matchesSymmetric = (a, b) => {
const keysA = Object.keys(a),
keysB = Object.keys(b);
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
... | [Two-sided comparison > Comparison by value]
### Comparison by value
For simple value comparison, you can again use the strict equality operator (`===`).
```js
const matchesSymmetric = (a, b) => {
const keysA = Object.keys(a),
keysB = Object.keys(b);
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
... | code_snippets | ||
e3181f85-819f-4853-8a2d-134abbe57339 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 4 | SemanticChunker@1.0.0 | 2f219302b75badb246bb85066996d0388f50fb1c4708c534c3a1237f148e2771 | [One-sided comparison > Two-sided comparison]
## Two-sided comparison
If you want to compare two objects in **both directions**, you can create a `Set` of the keys of both objects and check if the **size of the set** is equal to the number of keys in each object. Then, you can use the same approach as before, except ... | unknown | unknown | [One-sided comparison > Two-sided comparison]
## Two-sided comparison
If you want to compare two objects in **both directions**, you can create a `Set` of the keys of both objects and check if the **size of the set** is equal to the number of keys in each object. Then, you can use the same approach as before, except ... | [One-sided comparison > Two-sided comparison]
## Two-sided comparison
If you want to compare two objects in **both directions**, you can create a `Set` of the keys of both objects and check if the **size of the set** is equal to the number of keys in each object. Then, you can use the same approach as before, except ... | code_snippets | ||
ec8c563f-e284-475f-8893-3872ff1b5e5a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/match-object-properties.md | unknown | 2f00516c-e26d-4132-8184-e84f164fc554 | 2 | SemanticChunker@1.0.0 | 8db0b4fd4505594e79c55851e451bbc72afe5767f02028acef3fd8bb3dbfa1ea | [One-sided comparison > Comparison by value]
### Comparison by value
In order to compare the values of the properties, we can simply use the strict equality operator (`===`). This will ensure that the values are not only equal but also of the same type.
```js
const matches = (obj, source) =>
Object.keys(source).eve... | unknown | unknown | [One-sided comparison > Comparison by value]
### Comparison by value
In order to compare the values of the properties, we can simply use the strict equality operator (`===`). This will ensure that the values are not only equal but also of the same type.
```js
const matches = (obj, source) =>
Object.keys(source).eve... | [One-sided comparison > Comparison by value]
### Comparison by value
In order to compare the values of the properties, we can simply use the strict equality operator (`===`). This will ensure that the values are not only equal but also of the same type.
```js
const matches = (obj, source) =>
Object.keys(source).eve... | code_snippets | ||
cb427364-2817-49ec-bea4-f9e57aa3f8ce | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/make-iterable.md | unknown | a6c986b2-db9f-4335-b4fc-d545bd196749 | 0 | SemanticChunker@1.0.0 | 3331cfbcb4fb7bf0007a614bfbc631bb2d920579c5fb31a3a7d71edc226944ea | ---
title: Make any JavaScript value iterable
shortTitle: Make any value iterable
language: javascript
tags: [array,iterator,generator]
cover: rain-shopping
excerpt: Did you know you can define an iterator for any JavaScript value? This quick tip will show you how.
listed: true
dateModified: 2021-06-12
---
JavaScript'... | unknown | unknown | ---
title: Make any JavaScript value iterable
shortTitle: Make any value iterable
language: javascript
tags: [array,iterator,generator]
cover: rain-shopping
excerpt: Did you know you can define an iterator for any JavaScript value? This quick tip will show you how.
listed: true
dateModified: 2021-06-12
---
JavaScript'... | ---
title: Make any JavaScript value iterable
shortTitle: Make any value iterable
language: javascript
tags: [array,iterator,generator]
cover: rain-shopping
excerpt: Did you know you can define an iterator for any JavaScript value? This quick tip will show you how.
listed: true
dateModified: 2021-06-12
---
JavaScript'... | code_snippets | ||
b1334a3c-a840-44a5-bd91-542d62f874f9 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/luhn-check.md | unknown | 3d5f3f74-24e0-49ed-819c-5b8b26a1d889 | 0 | SemanticChunker@1.0.0 | 2eac110539b1221b1998db7ba895dc81985d3e1e918be9f04050b48c933a9b20 | ---
title: Implementing the Luhn Algorithm in JavaScript
shortTitle: Luhn algorithm
language: javascript
tags: [math,algorithm]
cover: blank-card
excerpt: Implement the Luhn Algorithm, used to validate a variety of identification numbers.
listed: true
dateModified: 2024-02-22
---
The [Luhn Algorithm](https://en.wikipe... | unknown | unknown | ---
title: Implementing the Luhn Algorithm in JavaScript
shortTitle: Luhn algorithm
language: javascript
tags: [math,algorithm]
cover: blank-card
excerpt: Implement the Luhn Algorithm, used to validate a variety of identification numbers.
listed: true
dateModified: 2024-02-22
---
The [Luhn Algorithm](https://en.wikipe... | ---
title: Implementing the Luhn Algorithm in JavaScript
shortTitle: Luhn algorithm
language: javascript
tags: [math,algorithm]
cover: blank-card
excerpt: Implement the Luhn Algorithm, used to validate a variety of identification numbers.
listed: true
dateModified: 2024-02-22
---
The [Luhn Algorithm](https://en.wikipe... | code_snippets | ||
008c2128-c91c-44f2-b2da-bc60edd4358d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 18 | SemanticChunker@1.0.0 | a750a160bfb69da13229d3cc950875a8074eb465066568dfde27a28745ab565a | [Parsing mathematical expressions > A more complex example]
```js
Sym.define(
'Expression',
'Operator',
'Term',
'ParenthesesOpen',
'ParenthesesClose',
'Function',
'NegativeSign'
);
const rules = [
// Non-terminal rules
// E -> T
new Rule(Sym.Expression, [Sym.Term]),
// E -> E op E
new Rule(
Sym.Expressio... | unknown | unknown | [Parsing mathematical expressions > A more complex example]
```js
Sym.define(
'Expression',
'Operator',
'Term',
'ParenthesesOpen',
'ParenthesesClose',
'Function',
'NegativeSign'
);
const rules = [
// Non-terminal rules
// E -> T
new Rule(Sym.Expression, [Sym.Term]),
// E -> E op E
new Rule(
Sym.Expressio... | [Parsing mathematical expressions > A more complex example]
```js
Sym.define(
'Expression',
'Operator',
'Term',
'ParenthesesOpen',
'ParenthesesClose',
'Function',
'NegativeSign'
);
const rules = [
// Non-terminal rules
// E -> T
new Rule(Sym.Expression, [Sym.Term]),
// E -> E op E
new Rule(
Sym.Expressio... | code_snippets | ||
057fab52-c767-492a-826f-0fd876534e05 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 0 | SemanticChunker@1.0.0 | 7894145bba3d4069977b9bada58a427b3731951c624081a003428ec8b9d303d3 | ---
title: Create a math expression parser in JavaScript
shortTitle: Math expression parser
language: javascript
tags: [math,algorithm]
cover: colorful-pots
excerpt: Building on top of everything we've tried in past articles, we'll use the Earley parsing algorithm to parse and evaluate math expressions.
listed: true
da... | unknown | unknown | ---
title: Create a math expression parser in JavaScript
shortTitle: Math expression parser
language: javascript
tags: [math,algorithm]
cover: colorful-pots
excerpt: Building on top of everything we've tried in past articles, we'll use the Earley parsing algorithm to parse and evaluate math expressions.
listed: true
da... | ---
title: Create a math expression parser in JavaScript
shortTitle: Math expression parser
language: javascript
tags: [math,algorithm]
cover: colorful-pots
excerpt: Building on top of everything we've tried in past articles, we'll use the Earley parsing algorithm to parse and evaluate math expressions.
listed: true
da... | code_snippets | ||
0d47fa8b-7cfe-4e0c-86e6-48695d4ff395 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 8 | SemanticChunker@1.0.0 | 06bac97187b62793fc0079be7098144a95d521ffe3d7c9f0451db36cb56eb449 | [Parsing mathematical expressions > Rules & symbols]
```js
Sym.define('P', 'S', 'M', 'T', '+', '*');
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M]),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T]),
// M → T
... | unknown | unknown | [Parsing mathematical expressions > Rules & symbols]
```js
Sym.define('P', 'S', 'M', 'T', '+', '*');
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M]),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T]),
// M → T
... | [Parsing mathematical expressions > Rules & symbols]
```js
Sym.define('P', 'S', 'M', 'T', '+', '*');
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M]),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T]),
// M → T
... | code_snippets | ||
104fa9f9-2a70-4d33-a2f3-896e069b1f36 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 5 | SemanticChunker@1.0.0 | 0eaab24372ddf24d3a7dfee65f4f2e42a78dea2916fa08570647ee836f43c6ba | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 4 | scan from S(4)(2) |
| 2 | M → M * T • | 2 | complete from (1) & S(4)(1) |
| 3 | M → M • * T | 2 | complete from (2) & S(2)(2) |
| 4 | S... | unknown | unknown | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 4 | scan from S(4)(2) |
| 2 | M → M * T • | 2 | complete from (1) & S(4)(1) |
| 3 | M → M • * T | 2 | complete from (2) & S(2)(2) |
| 4 | S... | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 4 | scan from S(4)(2) |
| 2 | M → M * T • | 2 | complete from (1) & S(4)(1) |
| 3 | M → M • * T | 2 | complete from (2) & S(2)(2) |
| 4 | S... | code_snippets | ||
1c48b761-9571-4d44-863f-4b608099b4e0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 10 | SemanticChunker@1.0.0 | 05f7e8ff448b07b028456c4090015ace39cb8d6330eae1243234f6e41669a081 | [Parsing mathematical expressions > Tokenization]
### Tokenization
Having extracted the **token matchers** from the CFG, we can feed them to a **tokenizer** that will take an **input string** and produce an **array of tokens**. We'll then feed these tokens to the Earley parser to build the AST.
```js title="tokenize... | unknown | unknown | [Parsing mathematical expressions > Tokenization]
### Tokenization
Having extracted the **token matchers** from the CFG, we can feed them to a **tokenizer** that will take an **input string** and produce an **array of tokens**. We'll then feed these tokens to the Earley parser to build the AST.
```js title="tokenize... | [Parsing mathematical expressions > Tokenization]
### Tokenization
Having extracted the **token matchers** from the CFG, we can feed them to a **tokenizer** that will take an **input string** and produce an **array of tokens**. We'll then feed these tokens to the Earley parser to build the AST.
```js title="tokenize... | code_snippets | ||
1f862949-75b8-47c6-ac62-02aa633a042d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 13 | SemanticChunker@1.0.0 | 33f882c161385355f21d02a55c94913b3c251309e021b3e0c6bc542ebea4771f | [Parsing mathematical expressions > AST nodes]
```
# [continued: part 1]
if (stateHasItem) return;
states[i].push({ ...earleyItem, position: earleyItem.position + 1 });
}
});
}
}
dfs(states, tokens) {
const root = states[0].reduce((best, curr) => {
if (best == null || curr.origin > best.origin) return curr;... | unknown | unknown | [Parsing mathematical expressions > AST nodes]
```
# [continued: part 1]
if (stateHasItem) return;
states[i].push({ ...earleyItem, position: earleyItem.position + 1 });
}
});
}
}
dfs(states, tokens) {
const root = states[0].reduce((best, curr) => {
if (best == null || curr.origin > best.origin) return curr;... | [Parsing mathematical expressions > AST nodes]
```
# [continued: part 1]
if (stateHasItem) return;
states[i].push({ ...earleyItem, position: earleyItem.position + 1 });
}
});
}
}
dfs(states, tokens) {
const root = states[0].reduce((best, curr) => {
if (best == null || curr.origin > best.origin) return curr;... | code_snippets | ||
2ced1531-8e71-4b6f-8380-124763b7b2be | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 9 | SemanticChunker@1.0.0 | d87f4f31a4837038674edd7309ba48aebb845bfb5bcacbc148a55830223d1127 | [Parsing mathematical expressions > Context-free grammar]
### Context-free grammar
Given a set of rules, we can now define a context-free grammar (CFG) class to hold the rules and provide methods for working with them. We'll extract the rules, symbols and **token matchers** when creating the CFG to make them easily a... | unknown | unknown | [Parsing mathematical expressions > Context-free grammar]
### Context-free grammar
Given a set of rules, we can now define a context-free grammar (CFG) class to hold the rules and provide methods for working with them. We'll extract the rules, symbols and **token matchers** when creating the CFG to make them easily a... | [Parsing mathematical expressions > Context-free grammar]
### Context-free grammar
Given a set of rules, we can now define a context-free grammar (CFG) class to hold the rules and provide methods for working with them. We'll extract the rules, symbols and **token matchers** when creating the CFG to make them easily a... | code_snippets | ||
2ff0d43e-7c2b-4847-959f-2403b5dacc7d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 16 | SemanticChunker@1.0.0 | b3bdbdd19d8891940e57115dca5c6eb12547d730358d338b96c5627c251f31ae | [Parsing mathematical expressions > Evaluation]
```js
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M], (left, _, right) => left + right),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T], (left, _, right) => left *... | unknown | unknown | [Parsing mathematical expressions > Evaluation]
```js
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M], (left, _, right) => left + right),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T], (left, _, right) => left *... | [Parsing mathematical expressions > Evaluation]
```js
const rules = [
// P → S
new Rule(Sym.P, [Sym.S]),
// S → S + M
new Rule(Sym.S, [Sym.S, Sym['+'], Sym.M], (left, _, right) => left + right),
// S → M
new Rule(Sym.S, [Sym.M]),
// M → M * T
new Rule(Sym.M, [Sym.M, Sym['*'], Sym.T], (left, _, right) => left *... | code_snippets | ||
33ba2ffd-0318-4860-8606-c1f0c224db82 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 1 | SemanticChunker@1.0.0 | a30fc2556fa0ebc1964d5434f2673fab7073dec75d7eea17a7ea5dd4754d860d | [Context-free grammars]
## Context-free grammars
A **context-free grammar** (CFG) is a set of rules that define the structure of a language. It consists of a set of **terminal symbols**, **non-terminal symbols**, a **start symbol**, and a set of **production rules**. The production rules define how the non-terminal s... | unknown | unknown | [Context-free grammars]
## Context-free grammars
A **context-free grammar** (CFG) is a set of rules that define the structure of a language. It consists of a set of **terminal symbols**, **non-terminal symbols**, a **start symbol**, and a set of **production rules**. The production rules define how the non-terminal s... | [Context-free grammars]
## Context-free grammars
A **context-free grammar** (CFG) is a set of rules that define the structure of a language. It consists of a set of **terminal symbols**, **non-terminal symbols**, a **start symbol**, and a set of **production rules**. The production rules define how the non-terminal s... | code_snippets | ||
3ad8b729-e6c8-470f-a499-ca2a3f90f648 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 20 | SemanticChunker@1.0.0 | 32aaf18a9a5f03aa4b036921967321a4777b20fd33d774ad3a6450e0d1620d4f | [Context-free grammars > Conclusion]
## Conclusion
In this article, we took a deep dive into the world of context-free grammars and the Earley parsing algorithm. We implemented a parser for mathematical expressions using these concepts, building an abstract syntax tree (AST) that we could evaluate to get the result o... | unknown | unknown | [Context-free grammars > Conclusion]
## Conclusion
In this article, we took a deep dive into the world of context-free grammars and the Earley parsing algorithm. We implemented a parser for mathematical expressions using these concepts, building an abstract syntax tree (AST) that we could evaluate to get the result o... | [Context-free grammars > Conclusion]
## Conclusion
In this article, we took a deep dive into the world of context-free grammars and the Earley parsing algorithm. We implemented a parser for mathematical expressions using these concepts, building an abstract syntax tree (AST) that we could evaluate to get the result o... | code_snippets | ||
4990008a-f555-404c-b872-27c821a65f81 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 3 | SemanticChunker@1.0.0 | a4770e3c3ab0eb067223de3495866042417b6611cf919283c0562a206d61be43 | [Earley parsing algorithm > Parsing example]
### Parsing example
Instead of going over the specifics of each step in detail, which I'm sure you can find in many other resources ([Wikipedia](https://en.wikipedia.org/wiki/Earley_parser) has a terrific lemma on the topic, that I based the below example on), we'll take a... | unknown | unknown | [Earley parsing algorithm > Parsing example]
### Parsing example
Instead of going over the specifics of each step in detail, which I'm sure you can find in many other resources ([Wikipedia](https://en.wikipedia.org/wiki/Earley_parser) has a terrific lemma on the topic, that I based the below example on), we'll take a... | [Earley parsing algorithm > Parsing example]
### Parsing example
Instead of going over the specifics of each step in detail, which I'm sure you can find in many other resources ([Wikipedia](https://en.wikipedia.org/wiki/Earley_parser) has a terrific lemma on the topic, that I based the below example on), we'll take a... | code_snippets | ||
5e28a1cb-47a2-4da3-a513-9740a53c758c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 6 | SemanticChunker@1.0.0 | ca14a5b46f7fb5b7a134e8f06b2c6874b7df9db90057919bfbf8d24736930918 | [Context-free grammars > Parsing mathematical expressions]
## Parsing mathematical expressions
Now that we have a basic understanding of the Earley parsing algorithm, let's implement a **parser for mathematical expressions** in JavaScript. We'll use the algorithm to parse the input string and build an **abstract synt... | unknown | unknown | [Context-free grammars > Parsing mathematical expressions]
## Parsing mathematical expressions
Now that we have a basic understanding of the Earley parsing algorithm, let's implement a **parser for mathematical expressions** in JavaScript. We'll use the algorithm to parse the input string and build an **abstract synt... | [Context-free grammars > Parsing mathematical expressions]
## Parsing mathematical expressions
Now that we have a basic understanding of the Earley parsing algorithm, let's implement a **parser for mathematical expressions** in JavaScript. We'll use the algorithm to parse the input string and build an **abstract synt... | code_snippets | ||
5fc05d58-d398-4687-b613-82a556ceb5b8 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 15 | SemanticChunker@1.0.0 | a8c9ed8f9a322d350c517f2f947fa127de45b35e38b73ca81cdd524ec5545261 | [Parsing mathematical expressions > Evaluation]
### Evaluation
With the AST in hand, we can now **evaluate the expression** by traversing the tree and performing the necessary operations. We'll add an `evaluator` to the `Rule` class, so that each rule can be evaluated, if an **evaluator function** is provided.
```js... | unknown | unknown | [Parsing mathematical expressions > Evaluation]
### Evaluation
With the AST in hand, we can now **evaluate the expression** by traversing the tree and performing the necessary operations. We'll add an `evaluator` to the `Rule` class, so that each rule can be evaluated, if an **evaluator function** is provided.
```js... | [Parsing mathematical expressions > Evaluation]
### Evaluation
With the AST in hand, we can now **evaluate the expression** by traversing the tree and performing the necessary operations. We'll add an `evaluator` to the `Rule` class, so that each rule can be evaluated, if an **evaluator function** is provided.
```js... | code_snippets | ||
a2434252-40ca-4955-b295-9c5df907b16f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 4 | SemanticChunker@1.0.0 | f252445b44c4c4526b757c17e708cc70a41e1b84375572e65374d5c84f331e91 | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 0 | scan from S(0)(6) |
| 2 | M → T • | 0 | complete from (1) & S(0)(5) |
| 3 | M → M • * T | 0 | complete from (2) & S(0)(4) |
| 4 | S → M... | unknown | unknown | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 0 | scan from S(0)(6) |
| 2 | M → T • | 0 | complete from (1) & S(0)(5) |
| 3 | M → M • * T | 0 | complete from (2) & S(0)(4) |
| 4 | S → M... | [Earley parsing algorithm > Parsing example]
| State No. | Production | Origin | Comment |
|-----------|--------------|--------|-----------------------------|
| 1 | T → number • | 0 | scan from S(0)(6) |
| 2 | M → T • | 0 | complete from (1) & S(0)(5) |
| 3 | M → M • * T | 0 | complete from (2) & S(0)(4) |
| 4 | S → M... | code_snippets | ||
a3520950-1ef7-4498-85d0-33545fb58bd0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 19 | SemanticChunker@1.0.0 | 3c399f1d1b4fbcb0dc5126690e819453135af0c2fe368498134aa62474027285 | [Parsing mathematical expressions > A more complex example]
> [!NOTE]
>
> If you couldn't tell, **I'm not very good at creating CFGs**. The above grammar is likely incorrect, but it should give you an idea of how you can extend the parser to handle more complex expressions. | unknown | unknown | [Parsing mathematical expressions > A more complex example]
> [!NOTE]
>
> If you couldn't tell, **I'm not very good at creating CFGs**. The above grammar is likely incorrect, but it should give you an idea of how you can extend the parser to handle more complex expressions. | [Parsing mathematical expressions > A more complex example]
> [!NOTE]
>
> If you couldn't tell, **I'm not very good at creating CFGs**. The above grammar is likely incorrect, but it should give you an idea of how you can extend the parser to handle more complex expressions. | code_snippets | ||
b93950b0-a045-45d9-8799-647c5e2b9b3e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 2 | SemanticChunker@1.0.0 | 928ab3f434c228d4d7988d19b14f8f6b2905d581d14c72c4f0c673929e662cb9 | [Context-free grammars > Earley parsing algorithm]
## Earley parsing algorithm
The **Earley parsing algorithm** is a general context-free parsing algorithm, that uses **dynamic programming** to parse strings according to a CFG. It's a top-down, predictive parser that can handle any CFG, including ambiguous grammars.
... | unknown | unknown | [Context-free grammars > Earley parsing algorithm]
## Earley parsing algorithm
The **Earley parsing algorithm** is a general context-free parsing algorithm, that uses **dynamic programming** to parse strings according to a CFG. It's a top-down, predictive parser that can handle any CFG, including ambiguous grammars.
... | [Context-free grammars > Earley parsing algorithm]
## Earley parsing algorithm
The **Earley parsing algorithm** is a general context-free parsing algorithm, that uses **dynamic programming** to parse strings according to a CFG. It's a top-down, predictive parser that can handle any CFG, including ambiguous grammars.
... | code_snippets | ||
d80f70ad-1e1f-4dfd-a08f-465f3b09698f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 14 | SemanticChunker@1.0.0 | 845f4637a54479c2395195dc1a3a9ed543e1cd67ff157bfe970f9aad8e2552a4 | [Parsing mathematical expressions > AST nodes]
```js
const parser = new EarleyParser(cfg);
const ast = parser.parse(tokens);
/*
{ type: 'S', children: [
{ type: 'S', children: [
{ type: 'M', children: [
{ type: 'T', value: '2' }
] },
] },
{ type: '+', value: '+' },
{ type: 'M', children: [
{ type: 'M', childr... | unknown | unknown | [Parsing mathematical expressions > AST nodes]
```js
const parser = new EarleyParser(cfg);
const ast = parser.parse(tokens);
/*
{ type: 'S', children: [
{ type: 'S', children: [
{ type: 'M', children: [
{ type: 'T', value: '2' }
] },
] },
{ type: '+', value: '+' },
{ type: 'M', children: [
{ type: 'M', childr... | [Parsing mathematical expressions > AST nodes]
```js
const parser = new EarleyParser(cfg);
const ast = parser.parse(tokens);
/*
{ type: 'S', children: [
{ type: 'S', children: [
{ type: 'M', children: [
{ type: 'T', value: '2' }
] },
] },
{ type: '+', value: '+' },
{ type: 'M', children: [
{ type: 'M', childr... | code_snippets | ||
e2bdf3e0-b72b-41a6-a5cc-50a6dfde55cb | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 7 | SemanticChunker@1.0.0 | 4d5a0d4cae4fd6ed08c6c857b9c794d778d0b0ec5803f8a6038304aa9c1d45dd | [Parsing mathematical expressions > Rules & symbols]
### Rules & symbols
Rules in a CFG are comprised of a **left-hand side** (LHS) and a **right-hand side** (RHS). The LHS is a **non-terminal symbol**, and the RHS is a **sequence of terminal and non-terminal symbols**. To facilitate this, we'll define a `Sym` class ... | unknown | unknown | [Parsing mathematical expressions > Rules & symbols]
### Rules & symbols
Rules in a CFG are comprised of a **left-hand side** (LHS) and a **right-hand side** (RHS). The LHS is a **non-terminal symbol**, and the RHS is a **sequence of terminal and non-terminal symbols**. To facilitate this, we'll define a `Sym` class ... | [Parsing mathematical expressions > Rules & symbols]
### Rules & symbols
Rules in a CFG are comprised of a **left-hand side** (LHS) and a **right-hand side** (RHS). The LHS is a **non-terminal symbol**, and the RHS is a **sequence of terminal and non-terminal symbols**. To facilitate this, we'll define a `Sym` class ... | code_snippets | ||
f1eb9257-f2bf-4e7b-8e48-005de96c2b4d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 12 | SemanticChunker@1.0.0 | 75861d2090726cfbef84d0a05fc2830f2bc18670e566163234e462551c201a26 | [Parsing mathematical expressions > AST nodes]
```
We'll see in a minute how this ties into the Earley parsing algorithm's results.
### Earley parser
Finally, we can implement the **Earley parser**, which will take the **tokens** produced by the tokenizer and build the AST using the CFG rules. The parser will retur... | unknown | unknown | [Parsing mathematical expressions > AST nodes]
```
We'll see in a minute how this ties into the Earley parsing algorithm's results.
### Earley parser
Finally, we can implement the **Earley parser**, which will take the **tokens** produced by the tokenizer and build the AST using the CFG rules. The parser will retur... | [Parsing mathematical expressions > AST nodes]
```
We'll see in a minute how this ties into the Earley parsing algorithm's results.
### Earley parser
Finally, we can implement the **Earley parser**, which will take the **tokens** produced by the tokenizer and build the AST using the CFG rules. The parser will retur... | code_snippets | ||
f32ee46b-d375-4a6a-96f9-c62a9757a973 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 17 | SemanticChunker@1.0.0 | fea6e4dd7dd7a7dca3dc0ed27eab194ee13d536746c9586c4c051af60952f194 | [Parsing mathematical expressions > A more complex example]
### A more complex example
Our parser is now capable of parsing and evaluating expressions, based on a context-free grammar. Thus, we can write a more complex grammar to handle mathematical expressions with parentheses, negative numbers and even some well-kn... | unknown | unknown | [Parsing mathematical expressions > A more complex example]
### A more complex example
Our parser is now capable of parsing and evaluating expressions, based on a context-free grammar. Thus, we can write a more complex grammar to handle mathematical expressions with parentheses, negative numbers and even some well-kn... | [Parsing mathematical expressions > A more complex example]
### A more complex example
Our parser is now capable of parsing and evaluating expressions, based on a context-free grammar. Thus, we can write a more complex grammar to handle mathematical expressions with parentheses, negative numbers and even some well-kn... | code_snippets | ||
f48692ce-ea20-4b18-869b-94a5554c2dee | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-parser.md | unknown | b0d05c2b-4c5a-4e90-bfdb-c0f97819e605 | 11 | SemanticChunker@1.0.0 | dd3d7f10f80ebf9d7577c1088ab261b2c1994b242eb6a80f54b74a896f287ae4 | [Parsing mathematical expressions > AST nodes]
### AST nodes
Before we can write our parser, we'll have to define an `ASTNode` class, similar to the previous article, so that we can store the **parsed tokens** in a **tree-like structure**.
```js title="astNode.js"
class ASTNode {
constructor({ item, children }) {
... | unknown | unknown | [Parsing mathematical expressions > AST nodes]
### AST nodes
Before we can write our parser, we'll have to define an `ASTNode` class, similar to the previous article, so that we can store the **parsed tokens** in a **tree-like structure**.
```js title="astNode.js"
class ASTNode {
constructor({ item, children }) {
... | [Parsing mathematical expressions > AST nodes]
### AST nodes
Before we can write our parser, we'll have to define an `ASTNode` class, similar to the previous article, so that we can store the **parsed tokens** in a **tree-like structure**.
```js title="astNode.js"
class ASTNode {
constructor({ item, children }) {
... | code_snippets | ||
e8c7d818-706b-488a-82e4-e71067ac5b90 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-string.md | unknown | a4c8c1a8-08cd-4bed-b4a4-8ee0ddb01f74 | 0 | SemanticChunker@1.0.0 | 1fb60a2bc31152091433d5b4a72668b552eded504c044322de99d69c72e53e7a | ---
title: Apply a mapping function to each character in a JavaScript string
shortTitle: Map string
language: javascript
tags: [string]
cover: budapest-palace
excerpt: Learn how to replicate the behavior of `Array.prototype.map()` for strings.
listed: true
dateModified: 2024-02-27
---
Ever wished `Array.prototype.map(... | unknown | unknown | ---
title: Apply a mapping function to each character in a JavaScript string
shortTitle: Map string
language: javascript
tags: [string]
cover: budapest-palace
excerpt: Learn how to replicate the behavior of `Array.prototype.map()` for strings.
listed: true
dateModified: 2024-02-27
---
Ever wished `Array.prototype.map(... | ---
title: Apply a mapping function to each character in a JavaScript string
shortTitle: Map string
language: javascript
tags: [string]
cover: budapest-palace
excerpt: Learn how to replicate the behavior of `Array.prototype.map()` for strings.
listed: true
dateModified: 2024-02-27
---
Ever wished `Array.prototype.map(... | code_snippets | ||
38498ef4-3fa5-4966-b263-efcb6cc6a4f0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-vs-object.md | unknown | be32e213-f031-487f-b4bb-34906b7a8908 | 1 | SemanticChunker@1.0.0 | 0cc666a2e4677e931aad4e278ec4c4b16cc6ce22c0e75e4e95dbb77a60f23c62 | [Key types]
## Key types
Object keys are limited to using only strings and symbols. Maps, on the other hand, can use values of any type as their keys, including functions and objects. This can come in handy in many different scenarios, such as memoization and data association.
```js
const people = [
{ id: 1, name: ... | unknown | unknown | [Key types]
## Key types
Object keys are limited to using only strings and symbols. Maps, on the other hand, can use values of any type as their keys, including functions and objects. This can come in handy in many different scenarios, such as memoization and data association.
```js
const people = [
{ id: 1, name: ... | [Key types]
## Key types
Object keys are limited to using only strings and symbols. Maps, on the other hand, can use values of any type as their keys, including functions and objects. This can come in handy in many different scenarios, such as memoization and data association.
```js
const people = [
{ id: 1, name: ... | code_snippets | ||
7d3547d3-2299-4546-9699-eef7687441f9 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-vs-object.md | unknown | be32e213-f031-487f-b4bb-34906b7a8908 | 3 | SemanticChunker@1.0.0 | a94e5c7fc93928f9433df653f7e93f25c525b87d0ea4ac0f9d55f48f6f15a593 | [Key types > Other differences]
## Other differences
Apart from the two main differences mentioned already, there are some other, less noticeable, ones. These include the following:
- Object size requires manual computation. Maps, on the other hand, have a built-in `size` property that can be used to track the numbe... | unknown | unknown | [Key types > Other differences]
## Other differences
Apart from the two main differences mentioned already, there are some other, less noticeable, ones. These include the following:
- Object size requires manual computation. Maps, on the other hand, have a built-in `size` property that can be used to track the numbe... | [Key types > Other differences]
## Other differences
Apart from the two main differences mentioned already, there are some other, less noticeable, ones. These include the following:
- Object size requires manual computation. Maps, on the other hand, have a built-in `size` property that can be used to track the numbe... | code_snippets | ||
9ed8e4d9-c5db-454e-8cca-80639d0ccbbe | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-vs-object.md | unknown | be32e213-f031-487f-b4bb-34906b7a8908 | 0 | SemanticChunker@1.0.0 | b18f4b88dfdec0a560c4220b12b0110ae2d83a1df6deb96bc6fb9214c0881290 | ---
title: What are the differences between Maps and objects in JavaScript?
shortTitle: Maps vs objects
language: javascript
tags: [object]
cover: tent-stars
excerpt: Maps and objects are very similar, but they have some differences that can help you decide which one better fits your use-case.
listed: true
dateModified... | unknown | unknown | ---
title: What are the differences between Maps and objects in JavaScript?
shortTitle: Maps vs objects
language: javascript
tags: [object]
cover: tent-stars
excerpt: Maps and objects are very similar, but they have some differences that can help you decide which one better fits your use-case.
listed: true
dateModified... | ---
title: What are the differences between Maps and objects in JavaScript?
shortTitle: Maps vs objects
language: javascript
tags: [object]
cover: tent-stars
excerpt: Maps and objects are very similar, but they have some differences that can help you decide which one better fits your use-case.
listed: true
dateModified... | code_snippets | ||
9eec3004-6dc4-45fe-9e60-11b2236811e9 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-vs-object.md | unknown | be32e213-f031-487f-b4bb-34906b7a8908 | 2 | SemanticChunker@1.0.0 | 65f8e0e1809c6feb6ec4ad0f3e362748bd83fecc8d2a846bb945e4d70075a106 | [Key types > Iteration]
## Iteration
Object iteration is usually accomplished using `Object.keys()`, `Object.values()` or `Object.entries()`. All of these methods are available on Maps as part of their prototype, but they are significantly more efficient. The reason for this is that these Map methods return iterators... | unknown | unknown | [Key types > Iteration]
## Iteration
Object iteration is usually accomplished using `Object.keys()`, `Object.values()` or `Object.entries()`. All of these methods are available on Maps as part of their prototype, but they are significantly more efficient. The reason for this is that these Map methods return iterators... | [Key types > Iteration]
## Iteration
Object iteration is usually accomplished using `Object.keys()`, `Object.values()` or `Object.entries()`. All of these methods are available on Maps as part of their prototype, but they are significantly more efficient. The reason for this is that these Map methods return iterators... | code_snippets | ||
146aae0d-b45e-482d-9d59-a4acc9d4a972 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-array-to-object.md | unknown | 83c991a0-4c9a-4b74-9e0b-95d7018773f1 | 2 | SemanticChunker@1.0.0 | b4d2e9201e3fd00c953484af2d7d3d1daf2f552214c9a6589558af05d0afa08c | [Map an array of primitives to an object > Map an array of objects to an object]
## Map an array of objects to an object
Subsequently, we can extend this snippet to map an **array of objects** to an object. This is done by mapping each object to a key and value, via a **pair of mapping functions**. The first mapping ... | unknown | unknown | [Map an array of primitives to an object > Map an array of objects to an object]
## Map an array of objects to an object
Subsequently, we can extend this snippet to map an **array of objects** to an object. This is done by mapping each object to a key and value, via a **pair of mapping functions**. The first mapping ... | [Map an array of primitives to an object > Map an array of objects to an object]
## Map an array of objects to an object
Subsequently, we can extend this snippet to map an **array of objects** to an object. This is done by mapping each object to a key and value, via a **pair of mapping functions**. The first mapping ... | code_snippets | ||
93e5da31-d51a-4b6f-97af-53bd988ba373 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-array-to-object.md | unknown | 83c991a0-4c9a-4b74-9e0b-95d7018773f1 | 1 | SemanticChunker@1.0.0 | ac556ecef52554aaaf367b87aad3d43ccbb92b36e7442ce603d6930a00942d75 | [Map an array of primitives to an object]
## Map an array of primitives to an object
An **array of primitives** may be transformed to an object by **mapping each element to a key and value**. The key is the element itself and the value is the result of applying a mapping function to the element.
To accomplish this, ... | unknown | unknown | [Map an array of primitives to an object]
## Map an array of primitives to an object
An **array of primitives** may be transformed to an object by **mapping each element to a key and value**. The key is the element itself and the value is the result of applying a mapping function to the element.
To accomplish this, ... | [Map an array of primitives to an object]
## Map an array of primitives to an object
An **array of primitives** may be transformed to an object by **mapping each element to a key and value**. The key is the element itself and the value is the result of applying a mapping function to the element.
To accomplish this, ... | code_snippets | ||
ef4c6e11-8961-475b-adb4-cd38457ddc45 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/map-array-to-object.md | unknown | 83c991a0-4c9a-4b74-9e0b-95d7018773f1 | 0 | SemanticChunker@1.0.0 | fece9b474b6562fc5a62b0a5353386fceddb0d0f85596bc2beb7fc8d8b062cae | ---
title: Map a JavaScript array to an object
shortTitle: Map array to object
language: javascript
tags: [array,object]
cover: two-lighthouses
excerpt: Map the values of an array to an object, using the given mapping functions.
listed: true
dateModified: 2024-01-15
---
A fairly common task I've written code for time ... | unknown | unknown | ---
title: Map a JavaScript array to an object
shortTitle: Map array to object
language: javascript
tags: [array,object]
cover: two-lighthouses
excerpt: Map the values of an array to an object, using the given mapping functions.
listed: true
dateModified: 2024-01-15
---
A fairly common task I've written code for time ... | ---
title: Map a JavaScript array to an object
shortTitle: Map array to object
language: javascript
tags: [array,object]
cover: two-lighthouses
excerpt: Map the values of an array to an object, using the given mapping functions.
listed: true
dateModified: 2024-01-15
---
A fairly common task I've written code for time ... | code_snippets | ||
6cf10821-4e93-43c0-a138-c8b677ae4829 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/mask-string.md | unknown | 5050c311-fcc6-42b8-8643-6f71f023c3eb | 0 | SemanticChunker@1.0.0 | f046aba8c6d89fa3c6a5daff105ce9ea1b7ec49a20ca23dff82082622f4e99cd | ---
title: Mask a JavaScript value
shortTitle: Mask value
language: javascript
tags: [string]
cover: rocky-beach-3
excerpt: Replace all characters in a string, except for the ending characters, with a mask character.
listed: true
dateModified: 2024-02-12
---
One of the most common bits of presentational logic related ... | unknown | unknown | ---
title: Mask a JavaScript value
shortTitle: Mask value
language: javascript
tags: [string]
cover: rocky-beach-3
excerpt: Replace all characters in a string, except for the ending characters, with a mask character.
listed: true
dateModified: 2024-02-12
---
One of the most common bits of presentational logic related ... | ---
title: Mask a JavaScript value
shortTitle: Mask value
language: javascript
tags: [string]
cover: rocky-beach-3
excerpt: Replace all characters in a string, except for the ending characters, with a mask character.
listed: true
dateModified: 2024-02-12
---
One of the most common bits of presentational logic related ... | code_snippets | ||
12e9131c-2e29-4f51-a72f-1f9efb182f0c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 1 | SemanticChunker@1.0.0 | f0608ca57bf3270c7548b0f501bc88f48635dc5bb051516422b5c6549e91bc39 | [Palindrome check]
## Palindrome check
Checking for a palindrome is straightforward. We can simply **reverse the string** and check if it is equal to the original string. Here's a simple implementation:
```js
const isPalindrome = str => {
const reversed = str.split('').reverse().join('');
return str === reversed;
... | unknown | unknown | [Palindrome check]
## Palindrome check
Checking for a palindrome is straightforward. We can simply **reverse the string** and check if it is equal to the original string. Here's a simple implementation:
```js
const isPalindrome = str => {
const reversed = str.split('').reverse().join('');
return str === reversed;
... | [Palindrome check]
## Palindrome check
Checking for a palindrome is straightforward. We can simply **reverse the string** and check if it is equal to the original string. Here's a simple implementation:
```js
const isPalindrome = str => {
const reversed = str.split('').reverse().join('');
return str === reversed;
... | code_snippets | ||
1a8419dd-3d7a-49b4-8957-e7ccd7ef9fe2 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 5 | SemanticChunker@1.0.0 | 956b857601f8cf261da74ca5f32c3e2273c49a099ee5b26b76769aa90609baed | [Palindrome check > Conclusion]
## Conclusion
We've seen how thinking through a solution thoroughly and using stepping stones can lead us to a more refined and efficient solution without needing to resort to extremely specialized algorithms. I hope I did the two-pointer technique justice, as it's used to solve a whol... | unknown | unknown | [Palindrome check > Conclusion]
## Conclusion
We've seen how thinking through a solution thoroughly and using stepping stones can lead us to a more refined and efficient solution without needing to resort to extremely specialized algorithms. I hope I did the two-pointer technique justice, as it's used to solve a whol... | [Palindrome check > Conclusion]
## Conclusion
We've seen how thinking through a solution thoroughly and using stepping stones can lead us to a more refined and efficient solution without needing to resort to extremely specialized algorithms. I hope I did the two-pointer technique justice, as it's used to solve a whol... | code_snippets | ||
4197cf73-cb55-4df8-9c9c-db6e16d1a857 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 4 | SemanticChunker@1.0.0 | 900d8d993693ea5709d620690f1e7f952625eb5893ff4b473088f99dd60048c3 | [Palindrome check > Efficient solution]
## Efficient solution
Using the previous expansion technique, we can thus **iterate over the string** and check for palindromes. For each position, we'll check for both **even and odd length palindromes**. The longest palindrome found will be our answer.
```js
const expandArou... | unknown | unknown | [Palindrome check > Efficient solution]
## Efficient solution
Using the previous expansion technique, we can thus **iterate over the string** and check for palindromes. For each position, we'll check for both **even and odd length palindromes**. The longest palindrome found will be our answer.
```js
const expandArou... | [Palindrome check > Efficient solution]
## Efficient solution
Using the previous expansion technique, we can thus **iterate over the string** and check for palindromes. For each position, we'll check for both **even and odd length palindromes**. The longest palindrome found will be our answer.
```js
const expandArou... | code_snippets | ||
5e8667ef-3624-4eb1-a8ae-02404c0a2ce7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 2 | SemanticChunker@1.0.0 | 5ffa48998728fa621af51b57e015c27ec444eb2a9dff7430c71b60bc25c76969 | [Palindrome check > Brute-force solution]
## Brute-force solution
Given the previous check, a **brute-force solution** would be to **check every possible substring** of the input string and see if it is a palindrome. While inefficient, this approach showcases a **two-pointer technique** that may be useful in similar ... | unknown | unknown | [Palindrome check > Brute-force solution]
## Brute-force solution
Given the previous check, a **brute-force solution** would be to **check every possible substring** of the input string and see if it is a palindrome. While inefficient, this approach showcases a **two-pointer technique** that may be useful in similar ... | [Palindrome check > Brute-force solution]
## Brute-force solution
Given the previous check, a **brute-force solution** would be to **check every possible substring** of the input string and see if it is a palindrome. While inefficient, this approach showcases a **two-pointer technique** that may be useful in similar ... | code_snippets | ||
9c21a1e0-79d3-4e90-85bb-9aa3be6902e1 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 3 | SemanticChunker@1.0.0 | ca5978e5d6402ba7aa1c74aaa0456e968d48361fbaf1cf07a4a06d68a1c8cf96 | [Palindrome check > Expanding around the center]
## Expanding around the center
Before I reveal a more efficient solution to the whole problem, I'd like to take a moment to find a **more efficient way to check if a string is a palindrome**.
Instead of reversing the string, we can **find its center** and expand outwa... | unknown | unknown | [Palindrome check > Expanding around the center]
## Expanding around the center
Before I reveal a more efficient solution to the whole problem, I'd like to take a moment to find a **more efficient way to check if a string is a palindrome**.
Instead of reversing the string, we can **find its center** and expand outwa... | [Palindrome check > Expanding around the center]
## Expanding around the center
Before I reveal a more efficient solution to the whole problem, I'd like to take a moment to find a **more efficient way to check if a string is a palindrome**.
Instead of reversing the string, we can **find its center** and expand outwa... | code_snippets | ||
c9edf178-0687-473e-bb92-56d9fca8e2a7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/longest-palindrome.md | unknown | eb2885ce-1352-47a0-b190-70c29dc88632 | 0 | SemanticChunker@1.0.0 | 96e48429714e1f4c21c8f63c6225a25673064b1e263e8fcaf322a08cba96ab74 | ---
title: How can I find the longest palindromic substring in a JavaScript string?
shortTitle: Longest palindromic substring
language: javascript
tags: [string,algorithm]
cover: flower-portrait-8
excerpt: While a brute-force longest palindrome finder is simple, it is not efficient. Let's explore a more efficient solut... | unknown | unknown | ---
title: How can I find the longest palindromic substring in a JavaScript string?
shortTitle: Longest palindromic substring
language: javascript
tags: [string,algorithm]
cover: flower-portrait-8
excerpt: While a brute-force longest palindrome finder is simple, it is not efficient. Let's explore a more efficient solut... | ---
title: How can I find the longest palindromic substring in a JavaScript string?
shortTitle: Longest palindromic substring
language: javascript
tags: [string,algorithm]
cover: flower-portrait-8
excerpt: While a brute-force longest palindrome finder is simple, it is not efficient. Let's explore a more efficient solut... | code_snippets | ||
028b3644-b00d-48f7-aba0-7feb263762b0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-tokenizer.md | unknown | 2b648894-b6f9-4ec0-b337-cd2bed3a53c3 | 11 | SemanticChunker@1.0.0 | 9832902927705ffbd6036a670e8435cfe8d693057d45f2210a33b6493ef265eb | [Introduction > Conclusion]
## Conclusion
I hope this article gave you a little peek into the world of tokenization and how simple logic can transform a string into a structured array of tokens. This is the basis for more complex problems, and understanding this process is crucial for building your own interpreters a... | unknown | unknown | [Introduction > Conclusion]
## Conclusion
I hope this article gave you a little peek into the world of tokenization and how simple logic can transform a string into a structured array of tokens. This is the basis for more complex problems, and understanding this process is crucial for building your own interpreters a... | [Introduction > Conclusion]
## Conclusion
I hope this article gave you a little peek into the world of tokenization and how simple logic can transform a string into a structured array of tokens. This is the basis for more complex problems, and understanding this process is crucial for building your own interpreters a... | code_snippets | ||
2fa4980f-b8f0-4e48-ae14-7c442720ef5b | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-tokenizer.md | unknown | 2b648894-b6f9-4ec0-b337-cd2bed3a53c3 | 0 | SemanticChunker@1.0.0 | 75d7a720c3d652e663d22a16b046f3e281459cc00be132fdb644eded17163003 | ---
title: Create a math expression tokenizer in JavaScript
shortTitle: Math expression tokenizer
language: javascript
tags: [math,algorithm]
cover: camper-school-bus
excerpt: Learn how to tokenize math expressions, using a simple JavaScript algorithm, forming the basis for more complex interpreters and compilers.
list... | unknown | unknown | ---
title: Create a math expression tokenizer in JavaScript
shortTitle: Math expression tokenizer
language: javascript
tags: [math,algorithm]
cover: camper-school-bus
excerpt: Learn how to tokenize math expressions, using a simple JavaScript algorithm, forming the basis for more complex interpreters and compilers.
list... | ---
title: Create a math expression tokenizer in JavaScript
shortTitle: Math expression tokenizer
language: javascript
tags: [math,algorithm]
cover: camper-school-bus
excerpt: Learn how to tokenize math expressions, using a simple JavaScript algorithm, forming the basis for more complex interpreters and compilers.
list... | code_snippets | ||
5ee23b27-1efe-4479-a1bb-4a7c6bc51ed8 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/math-expression-tokenizer.md | unknown | 2b648894-b6f9-4ec0-b337-cd2bed3a53c3 | 4 | SemanticChunker@1.0.0 | d89ec88c082a790f1dfe75ececfbd148af55081394d2eaf2711981c46a053c6d | [Introduction > Floating point numbers]
```js
tokenize('3.14 + 4');
// [
// { type: 'NUMBER', value: 3.14 },
// { type: 'OP_ADD' },
// { type: 'NUMBER', value: 4 }
// ]
tokenize('3.14.15 + 4');
// Error: Invalid character: .
``` | unknown | unknown | [Introduction > Floating point numbers]
```js
tokenize('3.14 + 4');
// [
// { type: 'NUMBER', value: 3.14 },
// { type: 'OP_ADD' },
// { type: 'NUMBER', value: 4 }
// ]
tokenize('3.14.15 + 4');
// Error: Invalid character: .
``` | [Introduction > Floating point numbers]
```js
tokenize('3.14 + 4');
// [
// { type: 'NUMBER', value: 3.14 },
// { type: 'OP_ADD' },
// { type: 'NUMBER', value: 4 }
// ]
tokenize('3.14.15 + 4');
// Error: Invalid character: .
``` | code_snippets |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.