diff --git a/docs/02_javascript/basic/ is-subsequence.md b/docs/02_javascript/basic/ is-subsequence.md deleted file mode 100644 index f7d0e8d..0000000 --- a/docs/02_javascript/basic/ is-subsequence.md +++ /dev/null @@ -1,59 +0,0 @@ ---- -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/operators.md b/docs/02_javascript/basic/operators.md new file mode 100644 index 0000000..82741bd --- /dev/null +++ b/docs/02_javascript/basic/operators.md @@ -0,0 +1,45 @@ +--- +sidebar_position: 1 +tags: + - operators +--- + +# Spread Operator… / Rest Parameter… + +***the rest parameter** is a feature that allows a function to accept an indefinite number of arguments* + +```jsx +// Rest parameter +var argumentsLength = function(...args) { + // 也可以把 array like args 轉 array 使用 [...args] + return args.length +}; + +argumentsLength(1,2,3) // 3 +``` + +***The spread operator** is used to unpack elements from an array or an iterable object (like a string or a set) into individual elements.* + +```jsx + +// Spread Operator +let arr = [3]; +let arr2 = [8, 9, 15]; + +let merged = [0, ...arr, 2, ...arr2]; + +alert(merged); // 0,3,2,8,9,15 + +// copy array +let arr = [1, 2, 3]; +let arrCopy = [...arr]; + +``` + +# 推薦閱讀 + +英文: Rest parameters and spread syntax () + +延伸考題 array-like object 跟 array 有什麼不同 ? + +像 function 裡的 arguments 是 array like object ,他可以使用 length、for…of、arguments[0] 等但卻不能使用 array 原生方法例如 push、forEach …等。若想要使用 array 原生方法必須使用 […arguments] 或Array.from() 轉成真正 array 才能使用