Skip to content

Commit 78747ed

Browse files
committed
Add JavaScript functions and categories
1 parent 97979c9 commit 78747ed

22 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
sidebar_position: 1
3+
tags:
4+
- bonus
5+
---
6+
7+
# isSubsequence (Bonus)
8+
9+
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.
10+
11+
### Example 1
12+
13+
```
14+
Input: nums = 'hello', 'hello world'
15+
Output: true
16+
17+
```
18+
19+
### Example 2
20+
21+
```
22+
Input: nums = 'sing', 'sting'
23+
Output: true
24+
```
25+
26+
### Example 3
27+
28+
```
29+
Input: nums = 'abc', 'abracadabra'
30+
Output: true
31+
```
32+
33+
### Example 4
34+
35+
```
36+
Input: nums = 'abc', 'acb'
37+
Output: false (order matters)
38+
```
39+
40+
### Solution
41+
42+
```jsx
43+
/**
44+
* @param {string} str1
45+
* @param {string} str2
46+
* @return {boolean}
47+
*/
48+
function isSubsequence(str1, str2) {
49+
let i = 0;
50+
let j = 0;
51+
if (!str1) return true;
52+
while (j < str2.length) {
53+
if (str1[i] === str2[j]) i++;
54+
if (i === str1.length) return true;
55+
j++;
56+
}
57+
return false;
58+
}
59+
```
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Bonus",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "5 minutes to learn the most important Docusaurus concepts."
7+
}
8+
}

docs/02_javascript/mock.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function numberCompare(a, b) {
2+
return a - b;
3+
}
4+
5+
[6, 4, 5, 10].sort(numberCompare);
6+
console.log([6, 4, 5, 10].sort(numberCompare)); // [4, 5, 6, 10]

0 commit comments

Comments
 (0)