@@ -48,8 +48,9 @@
- [Vue](basic/framework/vue.md)
- [React Redux](basic/framework/redux.md)
- [NativeScript](basic/framework/nativescript.md)
-- 算法入门(使用ES 6)
+- 算法入门(使用ES 6/7)
- [奇数求和](basic/algorithm/sum-of-odd-numbers.md)
+ - [查找单数](basic/algorithm/find-the-odd-int.md)
- [查找杂散数](basic/algorithm/find-the-stray-number.md)
## 经验篇
@@ -0,0 +1,31 @@
+# 查找散杂数
+
+## 题目 [^1]
+Given an array, find the int that appears an odd number of times.
+There will always be only one integer that appears an odd number of times.
+Examples:
+```
+[20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5] => 5
+[1,1,2,-2,5,2,4,4,-1,-2,5] => -1
+[^1]: <https://www.codewars.com/kata/find-the-odd-int>
+## 思路
+数组过滤,单数的特征是首次出现的下标和最后一次出现的下标差值为奇数。
+答案:
+```js
+const findOdd = arr => arr.sort().filter(x=>(arr.lastIndexOf(x)-arr.indexOf(x))%2===0)[0]
+其中:
+* `Array.prototype.filter`是ES 6的特性。
@@ -5,7 +5,7 @@
一般Redis里存储的数据需要一个默认的TTL,即到期删除,尽可能避免无用数据长期存储。
```js
-import redis from 'wulian-redis';
+const redis = require('@dwing/redis');
const client = redis({
host: '127.0.0.1',
@@ -2,16 +2,10 @@
## 前置条件
-### 1. Babel
+koa 2.0以上版本
```
-npm install -g babel-cli
-```
-
-### 2. koa 2.0以上版本
-npm install koa@v2.0.0-alpha.3
+npm install koa
(更新本文时的最新版本为2.0 alpha [^1])
@@ -44,14 +38,3 @@ app.use(ctx => {
app.listen(3000);
-***注:*** 需要Stage-3支持
-npm install babel-preset-stage-3
-执行:
-babel-node --presets stage-3 app.js
@@ -152,3 +152,18 @@ suite('iterator', function () {
`foreach` 和 `for ... of` 差不多。
`map` 性能最低。
+## 触发 react onchange 事件并赋值
+ var setValue = function (element, value) {
+ element.value = value;
+ if ('createEvent' in document) {
+ var event = new Event('input', { bubbles: true });
+ element.dispatchEvent(event);
+ }
+ else {
+ element.fireEvent('onchange');
+ };
@@ -159,3 +159,27 @@ Input Number Too Large
=============
1
+## What's Next?
+ES7 Async/Await:
+(async() => {
+ const result = await promiseFN();
+ console.log(result);
+})();
+多个异步方法优化:
+ const tasks = [];
+ tasks.push(promiseFN1());
+ tasks.push(promiseFN2());
+ tasks.push(promiseFN3());
+ const result = await Promise.all(tasks);
+ console.log(result[0], result[1], result[2]);
@@ -62,12 +62,12 @@ npm run test
### 安装 Yarn
-前置条件Brew(Mac OS X)
```bash
+# 前置条件Brew(Mac OS X)
brew update
brew install yarn
+# 或 直接使用npm
+npm i -g yarn
完成后用 `Sublime Text` 或其他编辑工具根据你使用的环境打开 `.zshrc` / `.bashrc` / `.profile`,添加一行:
@@ -105,7 +105,7 @@ data.rows.forEach(async (item) => {
`model/data.js` 片段:
-const { pool, format } = require('wulian-mysql');
+const { pool, format } = require('@dwing/mysql');
const { mysql: mysqlOptions } = require('../config');
const { isEmpty } = require('../lib');
@@ -44,7 +44,7 @@ for(let i = 0; i < xxx1.length; i++) {
exports.Func = async() => {
// 避免方法内require
- const redisClient = require('wulian-redis');
+ const redisClient = require('@dwing/redis');
// 没必要放在方法里,可以放到外边,多个方法共用
const redis = redisClient({
@@ -57,8 +57,8 @@ exports.Func = async() => {
## MySQL 编码细节
-import {pool} from 'wulian-mysql';
-import {isEmpty} from 'wulian-common';
+const {pool} = require('@dwing/mysql');
+const {isEmpty} = require('@dwing/common');
(async() => { // 包裹在async中
const client = await pool({ // mysql有await,redis没有
@@ -88,7 +88,7 @@ return result.affectedRows;
## Redis 编码细节
-import redisClient from 'wulian-redis';
+const redisClient = require('wulian-redis');
// config
});