├── MVVM ├── 1.html ├── 2.html ├── 3.html ├── index.html ├── index.js └── 类的继承 │ ├── index.js │ └── 构造继承.js ├── Queue.js ├── README.md ├── Stack.js ├── call.js ├── copy.js ├── linkedList.js ├── promise ├── index.js └── 类的继承 │ ├── index.js │ └── 构造继承.js ├── regular ├── index.js └── readme.md ├── sort ├── bubbleSort.js ├── insertionSort.js ├── quickSort.js ├── readme.md └── selectionSort.js ├── this.js ├── unique └── index.js ├── 两栏布局 └── index.html ├── 二叉tree.js ├── 前端武林秘籍.txt ├── 算法 ├── 十大排序 │ └── quickSort.js └── 字符串操作 │ ├── index.js │ ├── 判断回文字符串.js │ └── 翻转字符串.js ├── 类的继承 └── index.js ├── 红黑three.js └── 链表 └── index.js /MVVM/1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 访问器属性 8 | 9 | 10 | 11 | 14 | 28 | 29 | -------------------------------------------------------------------------------- /MVVM/2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 |
11 | {{text}} 12 |
13 | 14 | 22 | 首先将该任务分成几个子任务: 23 | 1.输入框以及文本节点与data中的数据绑定 24 | 2.输入框内容变化时,data中的数据同步变化。即view => model 的变化 25 | 3.data中的数据变化时,文本节点的内容同步变化。即model => view 26 | 27 | 28 | -------------------------------------------------------------------------------- /MVVM/3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | DocumentFragment 8 | 9 | 10 | 14 |
15 | 16 | 17 |
18 | 31 | 32 | -------------------------------------------------------------------------------- /MVVM/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 9 | 10 | 11 | 12 | 13 | 25 | 27 | 28 | -------------------------------------------------------------------------------- /MVVM/index.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WsmDyj/Interview/febf1f8aaa3c78142fb0f44515ceed64d8a9936a/MVVM/index.js -------------------------------------------------------------------------------- /MVVM/类的继承/index.js: -------------------------------------------------------------------------------- 1 | // 定义一个动物类 2 | function Animal (name) { 3 | // 属性 4 | this.name = name || 'Animal'; 5 | // 实例方法 6 | this.sleep = function(){ 7 | console.log(this.name + '正在睡觉!'); 8 | } 9 | } 10 | // 原型方法 11 | // Animal.prototype.eat = function(food) { 12 | // console.log(this.name + '正在吃:' + food); 13 | // }; 14 | 15 | // // 原型链继承 16 | // function Cat () {} 17 | // Cat.prototype = new Animal(); 18 | // Cat.prototype.name='cat'; 19 | // var cat = new Cat() 20 | 21 | 22 | 23 | // 构造继承 24 | 25 | // function Cat (name) { 26 | // Animal.call(this); 27 | // this.name = name || 'Tom'; 28 | // } 29 | 30 | 31 | // 组合继承 32 | // function Cat (name) { 33 | // Animal.call(this); 34 | // this.name = name || 'Tom'; 35 | // } 36 | // Cat.prototype = new Animal(); 37 | // Cat.prototype.constructor = Cat; 38 | // var cat = new Cat() 39 | 40 | 41 | // 寄生组合继承,在构造继承上加一个Super函数(没有实例和方法) 让他的原型链指向父类的原型链 42 | // 砍掉父类的实例属性,在调用两次父类的构造的时候,就不会初始化两次实例的方法/属性 43 | 44 | function Cat (name) { 45 | Animal.call(this); 46 | this.name = name || 'Tom' 47 | } 48 | 49 | (function () { 50 | // 创建一个没有实例方法的类 51 | var Super = function () {} 52 | Super.prototype = Animal.prototype; 53 | // 将实例作为子类的原型 54 | Cat.prototype = new Super() 55 | })() 56 | var cat = new Cat(); 57 | console.log(cat.name) 58 | // console.log(cat.sleep()) 59 | -------------------------------------------------------------------------------- /MVVM/类的继承/构造继承.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WsmDyj/Interview/febf1f8aaa3c78142fb0f44515ceed64d8a9936a/MVVM/类的继承/构造继承.js -------------------------------------------------------------------------------- /Queue.js: -------------------------------------------------------------------------------- 1 | // 队列 2 | function Queue() { 3 | var items = []; 4 | this.enqueue = function (element) { //向队列尾部添加一个新的项 5 | items.push(element); 6 | } 7 | this.dequeue = function() { // 移除队列的第一项,并返回被移除的元素 8 | return items.shift(); 9 | } 10 | this.front = function() { // 返回队列的第一个元素 11 | return items[0] 12 | } 13 | this.isEmpty = function() { //检查队列中是否有元素,返回true或false 14 | return items.length == 0; 15 | } 16 | this.size = function() { 17 | return items.length; 18 | } 19 | this.print = function() { 20 | console.log(items.toString()); 21 | } 22 | } 23 | 24 | // 使用Queue类 25 | var queue = new Queue(); 26 | console.log(queue.isEmpty()) 27 | queue.enqueue('John'); 28 | queue.enqueue('Jack'); 29 | queue.enqueue('Camial'); 30 | queue.print(); 31 | queue.dequeue(); 32 | queue.print(); 33 | 34 | //优先队列 35 | 36 | // 1.设置优先级,然后在正确的位置添加元素 37 | // 2.用入列操作添加元素,然后按照优先级移除他们。 38 | 39 | function priorityQueue() { 40 | var items = []; 41 | function QueueElement (element,priority) { 42 | this.element = element; 43 | this.priority = priority; 44 | } 45 | this.enqueue = function(element,priority){ 46 | var queueElement = new QueueElement(element,priority); 47 | 48 | if(this.isEmpty()) { 49 | items.push(queueElement); 50 | }else { 51 | var added = false; 52 | for (var i= 0;i 0) { 41 | rem = Math.floor(decNumber % 2); 42 | remStack.push(rem); 43 | decNumber = Math.floor(decNumber/2); 44 | } 45 | while(!remStack.isEmpty()) { 46 | binaryString += remStack.pop().toString(); 47 | } 48 | return binaryString; 49 | } 50 | console.log(divideBy2(233)) 51 | 52 | // 十进制转换成任意进制的基数为参数 53 | function baseConverter (decNumber,base) { 54 | var remStack = new Stack(), 55 | rem, 56 | baseString = '', 57 | digits = '0123456789ABCDEF'; 58 | while (decNumber > 0) { 59 | rem = Math.floor(decNumber % base); 60 | remStack.push(rem); 61 | decNumber = Math.floor(decNumber /base); 62 | } 63 | while (!remStack.isEmpty()){ 64 | baseString += digits[remStack.pop()]; 65 | } 66 | return baseString; 67 | } 68 | console.log(baseConverter(100345,2)) 69 | -------------------------------------------------------------------------------- /call.js: -------------------------------------------------------------------------------- 1 | 2 | // 不使用call、apply、bind,如何用js实现call或者apply功能 3 | 4 | // call() 方法在使用一个指定的this值和若干个指定的参数值的前提下条用某个函数或方法 5 | 6 | var foo = { 7 | value: 1 8 | } 9 | function bar() { 10 | console.log(this.value) 11 | } 12 | bar.call(foo) 13 | 14 | // call 改变了this的指向,指向foo 15 | // bar 函数执行了 16 | 17 | var foo = { 18 | value: 1, 19 | bar : function() { 20 | console.log(this.value) 21 | } 22 | } 23 | foo.bar(); 24 | 25 | // 1.将函数设为对象的属性 26 | // 2.执行该函数 27 | // 3.删除该函数 28 | 29 | Function.prototype.call2 = function(context) { 30 | context.fn = this; 31 | context.fn(); 32 | delete context.fn; 33 | } 34 | var foo = { 35 | value: 1 36 | } 37 | function bar () { 38 | console.log(this.value) 39 | } 40 | 41 | bar.call2(foo) 42 | 43 | // call函数还能给定参数执行函数 44 | var foo = { 45 | value: 1 46 | } 47 | function bar(name,age) { 48 | console.log(name) 49 | console.log(age) 50 | console.log(this.value) 51 | } 52 | bar.call(foo,'kevin',18); 53 | 54 | arguments = { 55 | 0:foo, 56 | 1:'kevin', 57 | 2:18, 58 | length: 3 59 | } 60 | var args = []; 61 | for (var i=0;i { 11 | if (this.status == 'pending') { 12 | this.status = 'resolve'; 13 | this.value = value; 14 | this.onResolvedCallbacks.forEach(fn => fn()) 15 | } 16 | } 17 | 18 | let reject = (reason) => { 19 | if (this.status == 'pending') { 20 | this.status = 'reject'; 21 | this.reason = reason; 22 | this.onRejectedCallbacks.forEach(fn => fn()) 23 | } 24 | } 25 | try{ 26 | executor(resolve, reject); 27 | } catch (err) { 28 | reject(err); 29 | } 30 | } 31 | then (onFullFilled,onRejected) { 32 | if (this.status == 'resolved') { 33 | onFullFilled(this.value) 34 | } 35 | if (this.status == 'rejectd') { 36 | onRejected(this.reason); 37 | } 38 | if (this.status == 'pending') { 39 | this.onResolvedCallbacks.push(()=>{ 40 | onFullFilled(this.value); 41 | }) 42 | this.onRejectedCallbacks.push(()=> { 43 | onRejected(this.reason); 44 | }) 45 | } 46 | 47 | } 48 | } 49 | 50 | const p = new Promise((resolve, reject) => { 51 | setTimeout(() => { 52 | resolve('hello world') 53 | }, 1000); 54 | }) 55 | p.then((data) =>{ 56 | console.log(data) 57 | },(err) =>{ 58 | console.log(err); 59 | }) -------------------------------------------------------------------------------- /promise/类的继承/index.js: -------------------------------------------------------------------------------- 1 | // 定义一个动物类 2 | function Animal (name) { 3 | // 属性 4 | this.name = name || 'Animal'; 5 | // 实例方法 6 | this.sleep = function(){ 7 | console.log(this.name + '正在睡觉!'); 8 | } 9 | } 10 | // 原型方法 11 | // Animal.prototype.eat = function(food) { 12 | // console.log(this.name + '正在吃:' + food); 13 | // }; 14 | 15 | // // 原型链继承 16 | // function Cat () {} 17 | // Cat.prototype = new Animal(); 18 | // Cat.prototype.name='cat'; 19 | // var cat = new Cat() 20 | 21 | 22 | 23 | // 构造继承 24 | 25 | // function Cat (name) { 26 | // Animal.call(this); 27 | // this.name = name || 'Tom'; 28 | // } 29 | 30 | 31 | // 组合继承 32 | // function Cat (name) { 33 | // Animal.call(this); 34 | // this.name = name || 'Tom'; 35 | // } 36 | // Cat.prototype = new Animal(); 37 | // Cat.prototype.constructor = Cat; 38 | // var cat = new Cat() 39 | 40 | 41 | // 寄生组合继承,在构造继承上加一个Super函数(没有实例和方法) 让他的原型链指向父类的原型链 42 | // 砍掉父类的实例属性,在调用两次父类的构造的时候,就不会初始化两次实例的方法/属性 43 | 44 | function Cat (name) { 45 | Animal.call(this); 46 | this.name = name || 'Tom' 47 | } 48 | 49 | (function () { 50 | // 创建一个没有实例方法的类 51 | var Super = function () {} 52 | Super.prototype = Animal.prototype; 53 | // 将实例作为子类的原型 54 | Cat.prototype = new Super() 55 | })() 56 | var cat = new Cat(); 57 | console.log(cat.name) 58 | // console.log(cat.sleep()) 59 | -------------------------------------------------------------------------------- /promise/类的继承/构造继承.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WsmDyj/Interview/febf1f8aaa3c78142fb0f44515ceed64d8a9936a/promise/类的继承/构造继承.js -------------------------------------------------------------------------------- /regular/index.js: -------------------------------------------------------------------------------- 1 | // 正则 2 | 3 | var pares_rul = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; 4 | 5 | var url = "http://www.ora.com:80/goodparts?q#fragment"; 6 | 7 | var result = pares_rul.exec(url); 8 | 9 | var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash']; 10 | 11 | var blanks = ' '; 12 | 13 | var i; 14 | 15 | for (i = 0; i< names.length; i+=1){ 16 | console.log(names[i]+':'+blanks.substring(names[i].length), result[i]); 17 | } 18 | 19 | 20 | // ^ 字符串的开始 ?可选 21 | -------------------------------------------------------------------------------- /regular/readme.md: -------------------------------------------------------------------------------- 1 | { m, n} 表示联系出现最少m次,最多出现n次 2 | 比如/ab{2,5}c/表示匹配这样一个字符串:第一个字符是“a”,接下来是2到5个字符“b”,最后是字符“c” 3 | 4 | [abc] 中间的任意一个 5 | 如果字符串特别多的话 可以使用 - 连字符 [123456abcdefGHIJKLM],可以写成[1-6a-fG-M] 6 | 7 | [^abc],表示是一个除"a"、"b"、"c"之外的任意一个字符。字符组的第一位放^(脱字符),表示求反的概念 8 | 9 | \d就是[0-9]。表示是一位数字。记忆方式:其英文是digit(数字) 10 | \D就是[^0-9]。表示除数字外的任意字符。 11 | \w就是[0-9a-zA-Z_]。表示数字、大小写字母和下划线。记忆方式:w是word的简写,也称单词字符。 12 | \W是[^0-9a-zA-Z_]。非单词字符。 13 | \s是[ \t\v\n\r\f]。表示空白符,包括空格、水平制表符、垂直制表符、换行符、回车符、换页符。记忆方式:s是space character的首字母。\S是[^ \t\v\n\r\f]。 非空白符。.就是[^\n\r\u2028\u2029]。通配符,表示几乎任意字符。换行符、回车符、行分隔符和段分隔符除外。记忆方式:想想省略号...中的每个点,都可以理解成占位符,表示任何类似的东西。 14 | 15 | -------------------------------------------------------------------------------- /sort/bubbleSort.js: -------------------------------------------------------------------------------- 1 | // 冒泡排序 2 | 事件复杂度O(n^2) 3 | //俩俩比较相邻记录的排序码,若发生逆序,则交换; 4 | // 有俩种方式进行冒泡,一种是先把小的冒泡到前边去,另一种是把大的元素冒泡到后边。 5 | 6 | 7 | // var swap = function (index1, index2) { 8 | // var aux = array[index1]; 9 | // array[index1] = array[index2]; 10 | // array[index2] = aux; 11 | // } 12 | 13 | // function createNodeSortedArray (size) { 14 | // var array = new ArrayList(); 15 | // for (let i=size; i>0; i--) { 16 | // array.insert(i); 17 | // } 18 | // return array; 19 | // } 20 | // function ArrayList () { //join方法拼接数组元素一个字符串,并返回该字符串 21 | // var array = []; 22 | // this.insert = function(item) { 23 | // array.push(item); 24 | // }; 25 | // this.toString = function() { 26 | // return array.join(); 27 | // } 28 | // this.bubbleSort = function () { 29 | // var length = array.length; 30 | // for (let i=0; i array[j+1]) { 33 | // swap (j, j+1); 34 | // } 35 | // } 36 | // } 37 | // }; 38 | // this.modifiedBubbleSort = function () { 39 | // var length = array.length; 40 | // for (let i=0; i array[j+1]); 43 | // swap(j,j+1); 44 | // } 45 | // } 46 | // } 47 | // } 48 | 49 | // var array = createNodeSortedArray(5); 50 | // console.log(array.toString()); 51 | // // array.bubbleSort(); 52 | // array.modifiedBubbleSort() 53 | // console.log(array.toString()); 54 | 55 | 56 | // 第一版本 57 | // function swap(arr, indexA, indexB) { 58 | // [arr[indexA], arr[indexB]] = [arr[indexB], arr[indexA]]; 59 | // } 60 | // function bubbleSort (arr) { 61 | // // for (let i=arr.length-1; i>0; i--) { 62 | // // for (let j=0; j arr[j + 1]) { 64 | // // swap(arr, j, j + 1); 65 | // // } 66 | // // } 67 | // // } 68 | // for (let i =0;i arr[j + 1]) { 71 | // swap(arr, j, j + 1); 72 | // } 73 | // } 74 | // } 75 | // return arr; 76 | // } 77 | // const arr = [91, 60, 96, 7, 35, 65, 10, 65, 9, 30, 20, 31, 77, 81, 24]; 78 | // console.log(bubbleSort(arr)); 79 | 80 | 81 | function swap(arr, indexA, indexB) { 82 | [arr[indexA], arr[indexB]] = [arr[indexB], arr[indexA]]; 83 | } 84 | function bubbleSort(arr, compareFunc) { 85 | for (let i = arr.length - 1; i > 0; i--) { 86 | for (let j = 0; j < i; j++) { 87 | if (compareFunc(arr[j], arr[j + 1]) > 0) { 88 | swap(arr, j, j + 1); 89 | } 90 | } 91 | } 92 | 93 | return arr; 94 | } 95 | 96 | // test 97 | const arr = [91, 60, 96, 7, 35, 65, 10, 65, 9, 30, 20, 31, 77, 81, 24]; 98 | console.log(bubbleSort(arr, (a, b) => a - b)); 99 | console.log(bubbleSort(arr, (a, b) => b - a)); -------------------------------------------------------------------------------- /sort/insertionSort.js: -------------------------------------------------------------------------------- 1 | // 插入排序 2 | 3 | function insertionSort (arr) { 4 | for (let i=0; i=0; j--) { 7 | var tmp = arr[j]; 8 | var order = tmp - element; 9 | if (order>0) { 10 | arr[j+1] = tmp; 11 | }else { 12 | break; 13 | } 14 | } 15 | arr[j+1] =element; 16 | } 17 | return arr; 18 | } 19 | 20 | var arr= [6,5,4,3,2,1]; 21 | console.log(insertionSort(arr)) -------------------------------------------------------------------------------- /sort/quickSort.js: -------------------------------------------------------------------------------- 1 | // 快速排序 2 | // 快速排序使用分治法把一个串(list)分为两个子串(sub-lists)。具体算法实现 3 | // 1.从数组中挑出一个元素,成为基准 4 | // 2.重新排列数组,所有元素比基准值小的摆放在基准前面,所有元素比基准大的摆在基准后面(相同的可以任意一边 5 | // 这个分区退出之后,该基准就处于数列的中间位置。成为分区操作 6 | // 3.递归的把小于基准值元素的子数列和大于基准值元素的子数列排序 7 | 8 | // 方法一、 9 | function quickSort (array, left, right) { 10 | if (Object.prototype.toString.call(array).slice(8, -1) === 'Array' && typeof left === 'number' && typeof right === 'number') { 11 | if (left < right) { 12 | var x = array[right], i = left - 1, temp; 13 | for (var j = left; j <= right; j++) { 14 | if (array[j] <= x) { 15 | i++; 16 | temp = array[i]; 17 | array[i] = array[j]; 18 | array[j] = temp; 19 | } 20 | } 21 | quickSort(array, left, i - 1); 22 | quickSort(array, i + 1, right); 23 | } 24 | // console.timeEnd('1.快速排序耗时'); 25 | return array; 26 | } else { 27 | return 'array is not an Array or left or right is not a number!'; 28 | } 29 | } 30 | // 方法二 31 | var quickSort2 = function (arr) { 32 | if (arr.length<=1){return arr} 33 | var pivotIndex = Math.floor(arr.length/2); 34 | var pivot = arr.slice(pivotIndex, 1)[0]; 35 | var left = []; 36 | var right = []; 37 | for (var i =0;ilen;j++) { 20 | if (arr[j] 0 ;i--) { 37 | let maxIndex = i; 38 | for (let j =i -1;j>=0; j --) { 39 | if (arr[j] >arr[maxIndex]) { 40 | maxIndex = j; 41 | } 42 | } 43 | if (i !== maxIndex) { 44 | swap(arr, i , maxIndex) 45 | } 46 | } 47 | return arr; 48 | } 49 | const arr = [91, 60, 96, 7, 35, 65, 10, 65, 9, 30, 20, 31, 77, 81, 24]; 50 | console.log(selectionSort2(arr)); -------------------------------------------------------------------------------- /this.js: -------------------------------------------------------------------------------- 1 | // 作为对象的方法调用 2 | // 作为普通函数调用 3 | // 构造器调用 4 | // Function.prototype.call 和 Function.prototype.apply 调用 5 | 6 | // 1.当函数作为对象的方法被调用时,this指向该对象 7 | var obj = { 8 | a: 1, 9 | getA : function() { 10 | console.log(this === obj); 11 | console.log(this.a) 12 | } 13 | } 14 | obj.getA(); 15 | 16 | // 2. -------------------------------------------------------------------------------- /unique/index.js: -------------------------------------------------------------------------------- 1 | // 数组去重 2 | var arr = [1, 5, 8, 1, 3, 5], 3 | len = arr.length; 4 | // 方法一 indexOf去重一 5 | // 数组的indexOf()方法可返回某个指定的元素在数组中首次出现的位置,没有就返回-1,该方法首先定义一个空数组res, 6 | // 然后调用indexOf方法对原来的数组进行遍历判断,如果不在res中,则将其push到res中,最后将res返回即可获得去重的数组 7 | 8 | // function unique(arr) { 9 | // if (!Array.isArray(arr)) { 10 | // console.log('type error!') 11 | // return 12 | // } 13 | // let res = []; 14 | // for (let i = 0; i < len; i++) { 15 | // if (res.indexOf(arr[i]) === -1) { 16 | // res.push(arr[i]) 17 | // } 18 | // } 19 | // return res 20 | // } 21 | 22 | // 方法二、indexOf去重二 23 | // 利用indexOf检查元素在数组中第一次出现的位置是否和元素现在的位置相等,如果不等则说明该元素是重复的 24 | // function unique (arr) { 25 | // if (!Array.isArray(arr)) { 26 | // console.log('type error!') 27 | // return 28 | // } 29 | // return Array.prototype.filter.call(arr, (item, index) => { 30 | // return arr.indexOf(item) === index; 31 | // }) 32 | // } 33 | 34 | // 方法三、相邻元素去重 35 | // 这种方法首先调用了数组的排序方法sort(),然后根据排序后的结果进行遍历及相邻元素对比,如果相同就跳过该元素 36 | // function unique (arr) { 37 | // if (!Array.isArray(arr)) { 38 | // console.log('type error') 39 | // return 40 | // } 41 | // arr = arr.sort() 42 | // let res = []; 43 | // for (let i = 0; i < len; i++) { 44 | // if (arr[i] !== arr [i-1]) { 45 | // res.push(arr[i]) 46 | // } 47 | // } 48 | // return res 49 | // } 50 | 51 | // 方法四、set与结构赋值去重 52 | // ES6新增了数据类型set,set的一个最大的有点就是数据不重复,set函数可以接受一个数组(或类数组对象)最为参数初始化 53 | // function unique (arr) { 54 | // if (!Array.isArray(arr)) { 55 | // console.log('type erroe') 56 | // return 57 | // } 58 | // return [...new Set(arr)] 59 | // } 60 | 61 | // 方法四、Array.from 与 Set 62 | // Array.from方法可以将Set结构转换为数组的结果 63 | // function unique (arr) { 64 | // return Array.from(new Set(arr)) 65 | // } 66 | 67 | // 方法五、双重循环 68 | // 先定义一个包含元素数组第一个元素的数组,然后遍历元素数组,将原始数组中的每一个元素与新数组中的每一个元素对比 69 | // ,如果重复则添加到新数组中,然后返回新数组,时间复杂度O(n ^2) 70 | 71 | function unique (arr) { 72 | if (!Array.isArray(arr)) { 73 | console.log('type error'); 74 | return 75 | } 76 | let res = [arr[0]]; 77 | for (let i = 0; i < len; i++) { 78 | let flag = true; 79 | for (let j = 0; j < res.length; j++) { 80 | if (arr[i] === res[j]) { 81 | flag = false; 82 | break 83 | } 84 | } 85 | if (flag) { 86 | res.push(arr[i]) 87 | } 88 | } 89 | return res 90 | } 91 | 92 | 93 | 94 | console.log(unique(arr)) 95 | -------------------------------------------------------------------------------- /两栏布局/index.html: -------------------------------------------------------------------------------- 1 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | Document 47 | 72 | 73 | 74 |
75 |
76 |
77 |
78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /二叉tree.js: -------------------------------------------------------------------------------- 1 | // 二叉数搜索 2 | // 创建自己的binarySearchTree类。首先,声明它的结构: 3 | // 树使用两个指针,一个指向左侧子节点,一个指向右侧子节点 4 | 5 | function BinarySearchTree() { 6 | var Node = function (key) { //声明一个Node类来表示书中的每一个节点 7 | this.key = key; //键是树相关的术语中对节点的称呼 8 | this.left = null; // 左指针指向左下一个节点 9 | this.right = null; // 右指针指向右下一个节点 10 | }; 11 | var root = null; //声明一个变量控制此数据结构的第一个节点,根元素 12 | 13 | // 向树中插入一个键 14 | this.insert = function(key) { 15 | var newNode = new Node(key); //1.创建用来表示node类型的实例. 16 | 17 | if(root==null){ //验证这个插入操作是否为一种特俗情况。我们要插入的节点是不是第一个节点。 18 | root = newNode; // 跟节点指向新节点。 19 | }else { 20 | insertNode(root,newNode); //跟节点加载非根节点的其他位置。 21 | } 22 | }; 23 | var insertNode = function(node,newNode){ // 如果树非空,需要找到插入新节点的位置。因此,在调用insertNode方法时通过参数传入树的根据点和要插入的节点 24 | if(newNode.key=0;i++) { 6 | tmp += str[i] 7 | } 8 | return tmp 9 | } 10 | 11 | reverseString('abcdfrg') 12 | 13 | // 方法二 装转化Array操作 14 | 15 | function reverseString(str) { 16 | var arr = str.split(''); 17 | var i =0,j=arr.length-1; 18 | while(i -1 && position < length) { 32 | let current = head, 33 | previous,index = 0; 34 | // 移除第一项 35 | if (position === 0) { 36 | head = current.next 37 | }else { 38 | while (index++ < position) { 39 | previous = current; 40 | current = current.next 41 | } 42 | // 将previous与current 43 | } 44 | } 45 | } 46 | } 47 | let list = new LinkedList() 48 | list.append (15) 49 | list.append(10) --------------------------------------------------------------------------------