Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sjfkai committed Feb 14, 2016
1 parent 4abda01 commit 3423200
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion _posts/zh_CN/2016-02-03-implementing-asynchronous-loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ for (var i=0; i<5; i++) {
for (let i=0; i<5; i++) {
var temp = i;
setTimeout(function(){
console.log(temp);
console.log(i);
}, 1000);
}
```
13 changes: 7 additions & 6 deletions _posts/zh_CN/2016-02-10-array-average-and-median.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: 数组平均值与中值
tip-number: 41
tip-username: soyuka
tip-username-profile: https://github.com/soyuka
tip-tldr: 计算数组的平均值与中值
tip-tldr: 计算数组的平均值与中位数


categories:
Expand Down Expand Up @@ -42,21 +42,22 @@ values /= count;

取得中值的步骤是:
- 将数组排序
- 取得中间的值
- 取得中位数

```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let middle = Math.floor(values.length / 2);
let median = values[middle];
// median = 23
let lowMiddle = Math.floor((values.length - 1) / 2);
let highMiddle = Math.ceil((values.length - 1) / 2);
let median = (values[lowMiddle] + values[highMiddle]) / 2;
// median = 13,5
```

或者使用[无符号右移](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Right_shift)操作符:

```javascript
let values = [2, 56, 3, 41, 0, 4, 100, 23];
values.sort((a, b) => a - b);
let median = values[values.length >> 1];
let median = (values[(values.length - 1) >> 1] + values[values.length >> 1]) / 2
// median = 23
```

0 comments on commit 3423200

Please sign in to comment.