This file contains hidden or 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
// Promise Implemenetation | |
// SOME CONSTANT | |
var https = require('https') | |
var URL1 = "https://jsonplaceholder.typicode.com/posts?userId=" | |
var URL2 = "https://jsonplaceholder.typicode.com/posts?id=" | |
// Phase 1: make function in 'then' deferred, and callback with resolved data later |
This file contains hidden or 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
<html lang="en"> | |
<head> | |
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> | |
<script src="https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script> | |
<script src="https://cdn.bootcss.com/backbone.js/1.3.3/backbone.js"></script> | |
</head> | |
<body> | |
<script> | |
// The difference between 'sync' event and 'sync' attribute in Backbone |
This file contains hidden or 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> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> | |
<script src="https://cdn.bootcss.com/underscore.js/1.8.3/underscore.js"></script> | |
<script src="https://cdn.bootcss.com/backbone.js/1.3.3/backbone.js"></script> | |
</head> | |
<body> |
This file contains hidden or 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 filterObject(obj, fn){ | |
return Object.keys(obj).filter((key) => { | |
var e = obj[key]; | |
return fn(e); | |
}).map((e) => { | |
return { | |
key: e, | |
value: obj[e] | |
}; | |
}); |
This file contains hidden or 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 str1 = "XMJYAUZ"; | |
var str2 = "MZJAWXU"; | |
var result = lcs(str1, str2); | |
console.log(result); | |
// result is { maxLength: 4, sequence: 'MJAU' } | |
function lcs(str1, str2){ | |
var maxLength = 0, maxCol = 0, maxRow = 0; | |
var sequence = ""; |
This file contains hidden or 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
/* 0-1 knapsack problem | |
For an overall introduction to knapsack problem, see https://en.wikipedia.org/wiki/Knapsack_problem | |
Function name: knapsack | |
Param: | |
items: an array of {w: v:} (where 'w' stands for weight, and 'v' stands for value) | |
capacity: a positive integer number | |
Will return max sum value that can reach, and the chosen subset to add up to the value. |
This file contains hidden or 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
// bitwise operation and utilities | |
// Left shift by 1, will multiply the number by 2 | |
console.log(2 << 1); // return 4 | |
// Right shift by 1, will divide the number by 2. | |
// In JavaScript, the decimal part will be floored | |
console.log(2 >> 1); // return 1 | |
console.log(3 >> 1); // return 1 |
This file contains hidden or 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
// Weighted directional graph implementation and Dijkstra's algorithm | |
class Graph { | |
constructor() { | |
this.vertices = []; | |
this.edges = []; | |
} | |
// add a sequence of vertices to the graph |
This file contains hidden or 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
// You are welcome to have a test on this function to check if it supports deep copy for very complex nested objects. | |
function deepCopy(o){ | |
if(typeof o === "object" && o !== null){ | |
var _o = new o.constructor(); | |
for(var k in o){ | |
_o[k] = deepCopy(o[k]); | |
} | |
return _o; | |
}else{ | |
return o; |
This file contains hidden or 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
/* | |
Permutation in mathmatics stands for all possible order for a set. | |
Combination in mathmatics stands for all possible ways of selecting items from a set. | |
Find more at | |
https://en.wikipedia.org/wiki/Permutation | |
https://en.wikipedia.org/wiki/Combination | |
*/ |