Fork me on GitHub

After code
  • Geeker
  • Gamer
  • JS
  • C
  • Node
  • React
  • Hippop
  • TDD
That Is An Byte of Me

[afterCode]read the f**king docs

13 Sep 2015

莎士比亚说 80%的bug,都可以通过阅读文档来解决


亚里士多德说 另外20%的bug 可以通过单元测试来定位

找bug

nshuffledCards 概率平均返回n张牌,如果n大于牌的总数,在返回全部随机排序的.

var nshuffledCards = (function () {
    var cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    function swap(i, j) {
        temp = cards[i]
        cards[i] = cards[j]
        cards[j] = temp
    }
    return function (n) {
        var _n = lo.min(n, cards.length)
        var result = []
        for (var i = 0; i < _n; i++) {
            var select = lo.random(i, _n - 1)
            swap(i, select)
            result.push(cards[i])
        }
        return result
    }
})()
console.log(nshuffledCards(1))

###再给点提示 如果执行

$ node untitled.js
FATAL ERROR: JS Allocation failed - process out of memory

读文档

_.min(collection, [iteratee], [thisArg])

Arguments

  1. collection (Array Object string): The collection to iterate over.
  2. [iteratee] (Function Object string): The function invoked per iteration.
  3. [thisArg] (*): The this binding of iteratee.

min传入的第一个参数是一个集合. 如果传入的参数不对会如何呢?

> lo.min(1,2)
Infinity

结论

如果你写代码,不看文档,又不写单元测试,那么bug无处不在. 别问我为什么知道这些.

分享到: QQ空间 新浪微博 腾讯微博 微信 更多
comments powered by Disqus