Quellcode durchsuchen

Gitbook Auto Published

willin vor 8 Jahren
Ursprung
Commit
781565b62e
7 geänderte Dateien mit 282 neuen und 0 gelöschten Zeilen
  1. 6 0
      SUMMARY.md
  2. 48 0
      basic/algorithm/find-the-stray-number.md
  3. 68 0
      basic/algorithm/sum-of-odd-numbers.md
  4. 57 0
      basic/js/map.md
  5. 59 0
      basic/js/reduce.md
  6. 30 0
      basic/js/regexp.md
  7. 14 0
      book.json

+ 6 - 0
SUMMARY.md

@@ -19,6 +19,9 @@
     - [小技巧](basic/js/tricks.md)
     - [Fetch AJAX](basic/js/fetch.md)
     - [ES Next](basic/js/es.md)
+    - [Map](basic/js/map.md)
+    - [Reduce](basic/js/reduce.md)
+    - [正则替换](basic/js/regexp.md)
   - Node.js
     - [包管理](basic/node/pkg.md)
     - [Hexo静态博客搭建](basic/node/hexo.md)
@@ -38,6 +41,9 @@
     - [NativeScript](basic/framework/nativescript.md)
     - [Koa](basic/framework/koa.md)
     - [React Redux](basic/framework/redux.md)
+  - 算法入门(使用ES 6)
+    - [奇数求和](basic/algorithm/sum-of-odd-numbers.md)
+    - [查找杂散数](basic/algorithm/find-the-stray-number.md)
 - [经验篇](experience/README.md)
   - [设计](experience/design/README.md)
     - [需求分析](experience/design/requirements.md)

+ 48 - 0
basic/algorithm/find-the-stray-number.md

@@ -0,0 +1,48 @@
+# 查找散杂数
+
+## 题目
+
+<https://www.codewars.com/kata/find-the-stray-number>
+
+You are given an odd-length array of integers, in which all of them are the same, except for one single number.
+
+Implement the method stray which accepts such array, and returns that single different number.
+
+The input array will always be valid! (odd-length >= 3)
+
+Examples:
+
+```
+[1, 1, 2] => 2
+
+[17, 17, 3, 17, 17, 17, 17] => 3
+```
+
+## 思路一
+
+数组过滤,散杂数的特征是首次出现的下标和最后一次出现的下标应保持一致(仅出现一次)。
+
+答案:
+
+```js
+const stray = arr => ~~arr.filter(x => arr.indexOf(x) === arr.lastIndexOf(x)).join('');
+```
+
+其中: 
+
+* `Array.prototype.filter`是ES 6的特性。
+* 结果为数组,先变成字符串(用`Array.prototype.join`),再强制类型转换为数字(用`~~`)
+
+## 思路二
+
+注意题干中提到了 `odd` 奇数个,可以用位运算来求不同值。
+
+答案:
+
+```js
+const stray = nums => nums.reduce((a, b) => a ^ b);
+```
+
+其中:
+
+* `Array.prototype.reduce` 为ES 6特性,可以参考 Map/Reduce 相关教程。

+ 68 - 0
basic/algorithm/sum-of-odd-numbers.md

@@ -0,0 +1,68 @@
+# 奇数求和
+
+## 题目
+
+<https://www.codewars.com/kata/sum-of-odd-numbers>
+
+Given the triangle of consecutive odd numbers:
+
+```
+             1
+          3     5
+       7     9    11
+   13    15    17    19
+21    23    25    27    29
+```
+
+Calculate the row sums of this triangle from the row index (starting at index 1) e.g.:
+
+```js
+rowSumOddNumbers(1); // 1
+rowSumOddNumbers(2); // 3 + 5 = 8
+```
+
+## 解题思路
+
+当成数学题来做。
+
+```
+第1行,1个数求和;
+第2行,2个数;
+第3行,3个数;
+
+......
+
+第N行,N个数。
+```
+
+每一行,首尾数分别为:
+
+```
+1: N
+2: N+1 2N+1
+3: 2N+1 3N+2
+
+......
+
+N: (N-1)*N+1 (N+1)*N-1
+```
+
+求和公式: 
+
+```
+(首项 + 末项) * 项数 / 2
+```
+
+即:
+
+```
+  ((N-1)*N+1 + (N+1)*N-1) * N / 2
+= 2 * N * N * N / 2
+= N^3   //(此处^不表示xor位运算,表示幂)
+```
+
+答案:
+
+```js
+const rowSumOddNumbers = n => Math.pow(n, 3);
+```

+ 57 - 0
basic/js/map.md

@@ -0,0 +1,57 @@
+# Map
+
+## Map 介绍
+
+### 语法
+
+```js
+array.map(callback[, thisArg])
+```
+
+### 参数
+
+#### callback
+
+原数组中的元素经过该方法后返回一个新的元素。
+
+> currentValue
+
+callback 的第一个参数,数组中当前被传递的元素。
+
+> index
+
+callback 的第二个参数,数组中当前被传递的元素的索引。
+
+> array
+
+callback 的第三个参数,调用 map 方法的数组。
+
+#### thisArg
+
+执行 callback 函数时 this 指向的对象。
+
+### 返回值
+
+由回调函数的返回值组成的新数组。
+
+## 例题
+
+<https://www.codewars.com/kata/double-char>
+
+Given a string, you have to return a string in which each character (case-sensitive) is repeated once.
+
+```
+doubleChar("String") ==> "SSttrriinngg"
+
+doubleChar("Hello World") ==> "HHeelllloo  WWoorrlldd"
+
+doubleChar("1234!_ ") ==> "11223344!!__  "
+```
+
+Good Luck!
+
+答案:
+
+```js
+const doubleChar = str => str.split('').map(i => i.repeat(2)).join('');
+```

+ 59 - 0
basic/js/reduce.md

@@ -0,0 +1,59 @@
+# Reduce
+
+## Reduce 介绍
+
+### 语法
+
+```js
+arr.reduce(callback,[initialValue]);
+```
+
+### 参数
+
+#### callback
+
+执行数组中每个值的函数,包含四个参数:
+
+> accumulator
+
+上一次调用回调返回的值,或者是提供的初始值(initialValue)
+
+> currentValue
+
+数组中正在处理的元素
+
+> currentIndex
+
+数据中正在处理的元素索引,如果没有提供initialValues,默认从0开始
+
+> array
+
+调用 reduce 的数组
+
+#### initialValue
+
+作为第一次调用 callback 的第一个参数。
+
+### 返回值
+
+函数累计处理的结果。
+
+## 例题
+
+<https://www.codewars.com/kata/beginner-reduce-but-grow>
+
+Given and array of integers (x), return the result of multiplying the values together in order. Example:
+
+```
+[1, 2, 3] --> 6
+```
+
+For the beginner, try to use the reduce method - it comes in very handy quite a lot so is a good one to know.
+
+Array will not be empty.
+
+答案:
+
+```js
+const grow = x => x.reduce((r, i) => r * i, 1);
+```

+ 30 - 0
basic/js/regexp.md

@@ -0,0 +1,30 @@
+# 正则替换
+
+## RegExp 对象
+
+参考: <https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp>
+
+工具:
+
+* RegExp tester (Chrome插件)
+
+## 例题
+
+<https://www.codewars.com/kata/create-phone-number>
+
+Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.
+
+Example:
+
+```
+createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"
+```
+
+The returned format must be correct in order to complete this challenge. 
+Don't forget the space after the closing parenthese!
+
+答案:
+
+```js
+const createPhoneNumber = n => n.join('').replace(/(\d{3})(\d{3})(\d{3})/,'($1) $2-$3');
+```

+ 14 - 0
book.json

@@ -19,6 +19,20 @@
       "书籍源码": "https://coding.net/u/willin/p/leader.js.cool/git"
     }
   },
+  "pdf": {
+    "toc": true,
+    "fontSize": 12,
+    "footerTemplate": null,
+    "headerTemplate": null,
+    "margin": {
+      "bottom": 36,
+      "left": 62,
+      "right": 62,
+      "top": 36
+    },
+    "pageNumbers": true,
+    "paperSize": "a4"
+  },
   "plugins": [
     "addcssjs",
     "autotheme",