Skip to content

Commit

Permalink
Add JavaScript functions and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
memoriaXII committed Mar 17, 2024
1 parent 97979c9 commit 78747ed
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 0 deletions.
59 changes: 59 additions & 0 deletions docs/02_javascript/basic/ is-subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
sidebar_position: 1
tags:
- bonus
---

# isSubsequence (Bonus)

Write a function called <code>isSubsequence</code> which takes in two strings and checks whether the characters in the first string form a subsequence of the characters in the second string. In other words, the function should check whether the characters in the first string appear somewhere in the second string, without their order changing.

### Example 1

```
Input: nums = 'hello', 'hello world'
Output: true
```

### Example 2

```
Input: nums = 'sing', 'sting'
Output: true
```

### Example 3

```
Input: nums = 'abc', 'abracadabra'
Output: true
```

### Example 4

```
Input: nums = 'abc', 'acb'
Output: false (order matters)
```

### Solution

```jsx
/**
* @param {string} str1
* @param {string} str2
* @return {boolean}
*/
function isSubsequence(str1, str2) {
let i = 0;
let j = 0;
if (!str1) return true;
while (j < str2.length) {
if (str1[i] === str2[j]) i++;
if (i === str1.length) return true;
j++;
}
return false;
}
```
8 changes: 8 additions & 0 deletions docs/02_javascript/basic/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Bonus",
"position": 3,
"link": {
"type": "generated-index",
"description": "5 minutes to learn the most important Docusaurus concepts."
}
}
6 changes: 6 additions & 0 deletions docs/02_javascript/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function numberCompare(a, b) {
return a - b;
}

[6, 4, 5, 10].sort(numberCompare);
console.log([6, 4, 5, 10].sort(numberCompare)); // [4, 5, 6, 10]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 78747ed

Please sign in to comment.