From 78747ed86bbee5a959b1b803ec8a2561d0150bf2 Mon Sep 17 00:00:00 2001 From: memoriaXII Date: Sun, 17 Mar 2024 12:38:13 +0800 Subject: [PATCH] Add JavaScript functions and categories --- docs/02_javascript/basic/ is-subsequence.md | 59 +++++++++++++++++++ docs/02_javascript/basic/_category_.json | 8 +++ docs/02_javascript/mock.js | 6 ++ .../recursion/_category_.json | 0 .../recursion/fib.md | 0 .../recursion/flatten.md | 0 .../recursion/fractional.md | 0 .../recursion/is-palindrome.md | 0 .../recursion/power.md | 0 .../recursion/product-of-array.md | 0 .../recursion/recursive-range.md | 0 .../recursion/reverse.md | 0 .../recursion/some-recursive.md | 0 .../recursion/sum-recursion.md | 0 .../search/_category_.json | 0 .../search/binary-search/binary-search.md | 0 .../search/linear-search/linear-search.md | 0 .../sorts/bubble-sort.md | 0 .../sorts/insertion-sort.md | 0 .../sorts/merge-sort.md | 0 .../sorts/quick-sort.md | 0 .../sorts/selection-sort.md | 0 22 files changed, 73 insertions(+) create mode 100644 docs/02_javascript/basic/ is-subsequence.md create mode 100644 docs/02_javascript/basic/_category_.json create mode 100644 docs/02_javascript/mock.js rename docs/{02_javascript => 03_algorithm}/recursion/_category_.json (100%) rename docs/{02_javascript => 03_algorithm}/recursion/fib.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/flatten.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/fractional.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/is-palindrome.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/power.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/product-of-array.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/recursive-range.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/reverse.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/some-recursive.md (100%) rename docs/{02_javascript => 03_algorithm}/recursion/sum-recursion.md (100%) rename docs/{02_javascript => 03_algorithm}/search/_category_.json (100%) rename docs/{02_javascript => 03_algorithm}/search/binary-search/binary-search.md (100%) rename docs/{02_javascript => 03_algorithm}/search/linear-search/linear-search.md (100%) rename docs/{02_javascript => 03_algorithm}/sorts/bubble-sort.md (100%) rename docs/{02_javascript => 03_algorithm}/sorts/insertion-sort.md (100%) rename docs/{02_javascript => 03_algorithm}/sorts/merge-sort.md (100%) rename docs/{02_javascript => 03_algorithm}/sorts/quick-sort.md (100%) rename docs/{02_javascript => 03_algorithm}/sorts/selection-sort.md (100%) diff --git a/docs/02_javascript/basic/ is-subsequence.md b/docs/02_javascript/basic/ is-subsequence.md new file mode 100644 index 0000000..f7d0e8d --- /dev/null +++ b/docs/02_javascript/basic/ is-subsequence.md @@ -0,0 +1,59 @@ +--- +sidebar_position: 1 +tags: + - bonus +--- + +# isSubsequence (Bonus) + +Write a function called isSubsequence 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; +} +``` diff --git a/docs/02_javascript/basic/_category_.json b/docs/02_javascript/basic/_category_.json new file mode 100644 index 0000000..4967902 --- /dev/null +++ b/docs/02_javascript/basic/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Bonus", + "position": 3, + "link": { + "type": "generated-index", + "description": "5 minutes to learn the most important Docusaurus concepts." + } +} diff --git a/docs/02_javascript/mock.js b/docs/02_javascript/mock.js new file mode 100644 index 0000000..8c97e0c --- /dev/null +++ b/docs/02_javascript/mock.js @@ -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] diff --git a/docs/02_javascript/recursion/_category_.json b/docs/03_algorithm/recursion/_category_.json similarity index 100% rename from docs/02_javascript/recursion/_category_.json rename to docs/03_algorithm/recursion/_category_.json diff --git a/docs/02_javascript/recursion/fib.md b/docs/03_algorithm/recursion/fib.md similarity index 100% rename from docs/02_javascript/recursion/fib.md rename to docs/03_algorithm/recursion/fib.md diff --git a/docs/02_javascript/recursion/flatten.md b/docs/03_algorithm/recursion/flatten.md similarity index 100% rename from docs/02_javascript/recursion/flatten.md rename to docs/03_algorithm/recursion/flatten.md diff --git a/docs/02_javascript/recursion/fractional.md b/docs/03_algorithm/recursion/fractional.md similarity index 100% rename from docs/02_javascript/recursion/fractional.md rename to docs/03_algorithm/recursion/fractional.md diff --git a/docs/02_javascript/recursion/is-palindrome.md b/docs/03_algorithm/recursion/is-palindrome.md similarity index 100% rename from docs/02_javascript/recursion/is-palindrome.md rename to docs/03_algorithm/recursion/is-palindrome.md diff --git a/docs/02_javascript/recursion/power.md b/docs/03_algorithm/recursion/power.md similarity index 100% rename from docs/02_javascript/recursion/power.md rename to docs/03_algorithm/recursion/power.md diff --git a/docs/02_javascript/recursion/product-of-array.md b/docs/03_algorithm/recursion/product-of-array.md similarity index 100% rename from docs/02_javascript/recursion/product-of-array.md rename to docs/03_algorithm/recursion/product-of-array.md diff --git a/docs/02_javascript/recursion/recursive-range.md b/docs/03_algorithm/recursion/recursive-range.md similarity index 100% rename from docs/02_javascript/recursion/recursive-range.md rename to docs/03_algorithm/recursion/recursive-range.md diff --git a/docs/02_javascript/recursion/reverse.md b/docs/03_algorithm/recursion/reverse.md similarity index 100% rename from docs/02_javascript/recursion/reverse.md rename to docs/03_algorithm/recursion/reverse.md diff --git a/docs/02_javascript/recursion/some-recursive.md b/docs/03_algorithm/recursion/some-recursive.md similarity index 100% rename from docs/02_javascript/recursion/some-recursive.md rename to docs/03_algorithm/recursion/some-recursive.md diff --git a/docs/02_javascript/recursion/sum-recursion.md b/docs/03_algorithm/recursion/sum-recursion.md similarity index 100% rename from docs/02_javascript/recursion/sum-recursion.md rename to docs/03_algorithm/recursion/sum-recursion.md diff --git a/docs/02_javascript/search/_category_.json b/docs/03_algorithm/search/_category_.json similarity index 100% rename from docs/02_javascript/search/_category_.json rename to docs/03_algorithm/search/_category_.json diff --git a/docs/02_javascript/search/binary-search/binary-search.md b/docs/03_algorithm/search/binary-search/binary-search.md similarity index 100% rename from docs/02_javascript/search/binary-search/binary-search.md rename to docs/03_algorithm/search/binary-search/binary-search.md diff --git a/docs/02_javascript/search/linear-search/linear-search.md b/docs/03_algorithm/search/linear-search/linear-search.md similarity index 100% rename from docs/02_javascript/search/linear-search/linear-search.md rename to docs/03_algorithm/search/linear-search/linear-search.md diff --git a/docs/02_javascript/sorts/bubble-sort.md b/docs/03_algorithm/sorts/bubble-sort.md similarity index 100% rename from docs/02_javascript/sorts/bubble-sort.md rename to docs/03_algorithm/sorts/bubble-sort.md diff --git a/docs/02_javascript/sorts/insertion-sort.md b/docs/03_algorithm/sorts/insertion-sort.md similarity index 100% rename from docs/02_javascript/sorts/insertion-sort.md rename to docs/03_algorithm/sorts/insertion-sort.md diff --git a/docs/02_javascript/sorts/merge-sort.md b/docs/03_algorithm/sorts/merge-sort.md similarity index 100% rename from docs/02_javascript/sorts/merge-sort.md rename to docs/03_algorithm/sorts/merge-sort.md diff --git a/docs/02_javascript/sorts/quick-sort.md b/docs/03_algorithm/sorts/quick-sort.md similarity index 100% rename from docs/02_javascript/sorts/quick-sort.md rename to docs/03_algorithm/sorts/quick-sort.md diff --git a/docs/02_javascript/sorts/selection-sort.md b/docs/03_algorithm/sorts/selection-sort.md similarity index 100% rename from docs/02_javascript/sorts/selection-sort.md rename to docs/03_algorithm/sorts/selection-sort.md