This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hanoi = function (n) { | |
var STATE_TABLE = [ | |
[2, 0, 1], | |
[1, 2, 0] | |
]; | |
var arr = [], len = n - 1, | |
curr_row, idx, | |
moves = []; | |
for (var i = n; i--;) arr[i] = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
"http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<title>Code container</title> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
<style type="text/css"> | |
.code-container { | |
width: 500px; | |
border: 1px solid #000000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array = { | |
new : function () { | |
var arr = [].slice.call(arguments); | |
arr.__proto__ = array.proto; | |
return arr; | |
}, | |
proto : { | |
__proto__ : Array.prototype, | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @example _.map | |
*/ | |
function map() { | |
//Here come the examples | |
//of _.map | |
} | |
/** | |
* @example _.bind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Isn't it? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unique(sortedArr) { | |
var arr = [], | |
i = -1, | |
j = -1, | |
len = sortedArr.length; | |
while (++i < len) { | |
if (arr[j] !== sortedArr[i]) { | |
arr[++j] = sortedArr[i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repeat(obj, times) { | |
var res = typeof obj == 'string' ? '' : []; | |
times = Math.max(1, times); | |
do { | |
if (times & 0x1) res = res.concat(obj); | |
obj = obj.concat(obj); | |
} while ((times >>= 1)); | |
return res; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.new = function () { | |
var ctor = this; | |
function F(){return ctor.apply(this, arguments);} | |
F.prototype = ctor.prototype; | |
return new F; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function range(start, end, step) { | |
step = Math.floor(Math.abs(step)) || 1; | |
if (typeof end == 'undefined') { | |
end = start; | |
start = 0; | |
} | |
else if (start > end) { | |
var s = start; | |
start = end; | |
end = s; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function valueRange(start, end) { | |
return function (value) { | |
return Math.min(Math.max(value, start), end); | |
}; | |
} | |
//Get new value range [0, 20] | |
var f = valueRange(0, 20); | |
console.log(f(10)); //10 |