diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index f52bbce..751b943 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,11 @@ language: node_js node_js: - "6" - addons: chrome: stable - -before_script: - - "export DISPLAY=:99.0" - - "sh -e /etc/init.d/xvfb start" - - sleep 3 # give xvfb some time to start - +services: + - xvfb before_install: - npm install karma-cli -g \ No newline at end of file + npm install karma-cli -g +script: + - npm run start diff --git a/js/list.js b/js/list.js index 356bcf9..6b8a01b 100755 --- a/js/list.js +++ b/js/list.js @@ -1,34 +1,35 @@ -/** - * List - * @author donaldyang - */ - -function List(head, tail) { - this.head = head || 0; - this.tail = tail || null; -} - -// Returns a new List containing the array. -List.list = function (array) { - var sentinel = new List(), - len = array.length, - p, i; - - p = sentinel; - for (i = 0; i < len; i++) { - p.tail = new List(array[i]); - p = p.tail; - } - return sentinel.tail; -} - -// Returns a readable String for THIS. -List.prototype.toString = function () { - var res = '', L; - res += '['; - for (L = this; L !== null; L = L.tail) { - res = res + ' ' + L.head; - } - res += ' ]'; - return res; -}; +/** + * List + * @author donaldyang + */ + +function List(head, tail) { + this.head = head || 0; + this.tail = tail || null; +} + +// Returns a new List containing the array. [4, 6, 7, 3, 8] +List.list = function (array) { + var sentinel = new List(), + len = array.length, + p, i; + + p = sentinel; + for (i = 0; i < len; i++) { + p.tail = new List(array[i]); + p = p.tail; + } + return sentinel.tail; +}; + +// Returns a readable String for THIS. +List.prototype.toString = function () { + var res = '', L; + res += '['; + for (L = this; L !== null; L = L.tail) { + res = res + ' ' + L.head; + } + res += ' ]'; + return res; +}; + diff --git a/package.json b/package.json index e80646a..da4fdf8 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "test", "main": "index.js", "scripts": { - "test": "karma start" + "test": "karma start", + "start": "karma start --single-run" }, "repository": { "type": "git", diff --git a/quz/quz.js b/quz/quz.js index 624e7de..443bba2 100755 --- a/quz/quz.js +++ b/quz/quz.js @@ -1,27 +1,48 @@ -/** - * dcate - * A list consisting of elements of A followed by the - * elements of B. May modify items of A. - * Don't use 'new' - * @param {List} A - * @param {List} B - * @returns {List} - */ -function dcate(A, B) { - /** Fill in here **/ -} - -/** - * sub - * The sublist consisting of LEN items from list L, - * beginning with item #START (where the first item is #0). - * Does not modify the original list elements. - * it is an error if the desired items don't exist. - * @param {List} L - * @param {Number} start - * @param {Number} len - * @returns {List} - */ -function sub(L, start, len) { - /** Fill in here **/ -} +/** + * dcate + * A list consisting of elements of A followed by the + * elements of B. May modify items of A. + * Don't use 'new' + * @param {List} A + * @param {List} B + * @returns {List} + */ +// var A = List.list([4, 6, 7, 3, 8]), +// B = List.list([3, 2, 5, 9]), +// C = List.list([19, 8, 7, 3, 2]); +function dcate(A, B) { + let p = A; + // p:A中的最末尾元素 + while (p.tail) { + p = p.tail + } + // 将B添加到A的尾巴 + p.tail = B; + return A +} + +/** + * sub + * The sublist consisting of LEN items from list L, + * beginning with item #START (where the first item is #0). + * Does not modify the original list elements. + * it is an error if the desired items don't exist. + * @param {List} L + * @param {Number} start + * @param {Number} len + * @returns {List} + */ +function sub(L, start, len) { + let p = L; // 当前遍历的元素 + let i = 0; // 第N个元素 + let array = []; + while (p && p.head) { + // 当元素处于开始到结束之间,array增加head属性 + if (i >= start && i <= start + len) { + array.push(p.head) + } + p = p.tail; + i++ + } + return List.list(array) +}