From 1f729bac837f8d7b73f44a83d5a33e842a2703b3 Mon Sep 17 00:00:00 2001 From: Xiaoyun Date: Mon, 20 Aug 2018 17:34:18 +0200 Subject: [PATCH] finished --- package-lock.json | 3 + starter-code/package-lock.json | 320 ++++++++++++++++++++++++++++ starter-code/src/array-functions.js | 49 +++-- starter-code/src/bubble-sort.js | 8 +- starter-code/src/merge-sort.js | 10 +- starter-code/src/sequencer.js | 110 +++++----- 6 files changed, 421 insertions(+), 79 deletions(-) create mode 100644 package-lock.json create mode 100644 starter-code/package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/starter-code/package-lock.json b/starter-code/package-lock.json new file mode 100644 index 0000000..39bf3d7 --- /dev/null +++ b/starter-code/package-lock.json @@ -0,0 +1,320 @@ +{ + "name": "mocha-practice", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "browser-stdout": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", + "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=", + "dev": true + }, + "chai": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", + "dev": true, + "requires": { + "assertion-error": "^1.0.1", + "deep-eql": "^0.1.3", + "type-detect": "^1.0.0" + } + }, + "commander": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.9.0.tgz", + "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", + "dev": true, + "requires": { + "graceful-readlink": ">= 1.0.0" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "debug": { + "version": "2.6.8", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz", + "integrity": "sha1-5zFTHKLt4n0YgiJCfaF4IdaP9Pw=", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-eql": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", + "dev": true, + "requires": { + "type-detect": "0.1.1" + }, + "dependencies": { + "type-detect": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz", + "integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=", + "dev": true + } + } + }, + "diff": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.2.0.tgz", + "integrity": "sha1-yc45Okt8vQsFinJck98pkCeGj/k=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "glob": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.1.tgz", + "integrity": "sha1-gFIR3wT6rxxjo2ADBs31reULLsg=", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.2", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "graceful-readlink": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/graceful-readlink/-/graceful-readlink-1.0.1.tgz", + "integrity": "sha1-TK+tdrxi8C+gObL5Tpo906ORpyU=", + "dev": true + }, + "growl": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz", + "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=", + "dev": true + }, + "has-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-1.0.0.tgz", + "integrity": "sha1-nZ55MWXOAXoA8AQYxD+UKnsdEfo=", + "dev": true + }, + "he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", + "dev": true + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true + }, + "json3": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/json3/-/json3-3.3.2.tgz", + "integrity": "sha1-PAQ0dD35Pi9cQq7nsZvLSDV19OE=", + "dev": true + }, + "lodash._baseassign": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz", + "integrity": "sha1-jDigmVAPIVrQnlnxci/QxSv+Ck4=", + "dev": true, + "requires": { + "lodash._basecopy": "^3.0.0", + "lodash.keys": "^3.0.0" + } + }, + "lodash._basecopy": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz", + "integrity": "sha1-jaDmqHbPNEwK2KVIghEd08XHyjY=", + "dev": true + }, + "lodash._basecreate": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash._basecreate/-/lodash._basecreate-3.0.3.tgz", + "integrity": "sha1-G8ZhYU2qf8MRt9A78WgGoCE8+CE=", + "dev": true + }, + "lodash._getnative": { + "version": "3.9.1", + "resolved": "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz", + "integrity": "sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=", + "dev": true + }, + "lodash._isiterateecall": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/lodash._isiterateecall/-/lodash._isiterateecall-3.0.9.tgz", + "integrity": "sha1-UgOte6Ql+uhCRg5pbbnPPmqsBXw=", + "dev": true + }, + "lodash.create": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/lodash.create/-/lodash.create-3.1.1.tgz", + "integrity": "sha1-1/KEnw29p+BGgruM1yqwIkYd6+c=", + "dev": true, + "requires": { + "lodash._baseassign": "^3.0.0", + "lodash._basecreate": "^3.0.0", + "lodash._isiterateecall": "^3.0.0" + } + }, + "lodash.isarguments": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", + "integrity": "sha1-L1c9hcaiQon/AGY7SRwdM4/zRYo=", + "dev": true + }, + "lodash.isarray": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/lodash.isarray/-/lodash.isarray-3.0.4.tgz", + "integrity": "sha1-eeTriMNqgSKvhvhEqpvNhRtfu1U=", + "dev": true + }, + "lodash.keys": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/lodash.keys/-/lodash.keys-3.1.2.tgz", + "integrity": "sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo=", + "dev": true, + "requires": { + "lodash._getnative": "^3.0.0", + "lodash.isarguments": "^3.0.0", + "lodash.isarray": "^3.0.0" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, + "requires": { + "minimist": "0.0.8" + } + }, + "mocha": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-3.5.3.tgz", + "integrity": "sha512-/6na001MJWEtYxHOV1WLfsmR4YIynkUEhBwzsb+fk2qmQ3iqsi258l/Q2MWHJMImAcNpZ8DEdYAK72NHoIQ9Eg==", + "dev": true, + "requires": { + "browser-stdout": "1.3.0", + "commander": "2.9.0", + "debug": "2.6.8", + "diff": "3.2.0", + "escape-string-regexp": "1.0.5", + "glob": "7.1.1", + "growl": "1.9.2", + "he": "1.1.1", + "json3": "3.3.2", + "lodash.create": "3.1.1", + "mkdirp": "0.5.1", + "supports-color": "3.1.2" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "supports-color": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-3.1.2.tgz", + "integrity": "sha1-cqJiiU2dQIuVbKBf83su2KbiotU=", + "dev": true, + "requires": { + "has-flag": "^1.0.0" + } + }, + "type-detect": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz", + "integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=", + "dev": true + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/starter-code/src/array-functions.js b/starter-code/src/array-functions.js index ec99c6a..b5ca19a 100644 --- a/starter-code/src/array-functions.js +++ b/starter-code/src/array-functions.js @@ -1,22 +1,43 @@ -var ArrayFunctions = function (){} +class ArrayFunctions { + // constructor() { + // } + printSpecial(array) { + return array.join(" --- "); + } + + doubleArray(array) { + return array.map(number => number * 2) + } + + superPower(array){ + return array.reduce((sum, number, index) => sum + (number * (Math.pow(10, index))) + ) + } +} + + + + + +// var ArrayFunctions = function (){} // 1) Define a function that takes an array with numbers and prints all the elements of the array, separated by " --- " // ArrayFunctions.printSpecial([12, 33, 144, 122]) // 12 -- 33 -- 144 -- 122 -ArrayFunctions.prototype.printSpecial = function (array) { - return array.join(" --- "); -}; +// ArrayFunctions.prototype.printSpecial = function (array) { +// return array.join(" --- "); +// }; // 2) Define a function that takes an array with numbers and returns another array where each element contains double each element in the array // ArrayFunctions.doubleMyArray([10, 20, 35, 12]) // [20, 40, 70, 24] -ArrayFunctions.prototype.doubleArray = function(array){ - return array.map(function(number){ - return number * 2; - }); -}; +// ArrayFunctions.prototype.doubleArray = function(array){ +// return array.map(function(number){ +// return number * 2; +// }); +// }; // 3) Define a function that takes an array with numbers and returns the result of multiplying each element by ten to the power of the position it's in: // ArrayFunctions.superPower([1,2,3,4,5]) @@ -24,10 +45,10 @@ ArrayFunctions.prototype.doubleArray = function(array){ // explanation: (1 x 10^0) + (2 x 10^1) + (3 x 10^2) + (4 x 10^3) + (5 x 10^4) // explanation: (1) + (20) + (300) + (4000) + (50000) -ArrayFunctions.prototype.superPower = function(array){ - return array.reduce(function(sum, number, index){ - return sum + (number * (Math.pow(10, index))); - }); -} +// ArrayFunctions.prototype.superPower = function(array){ +// return array.reduce(function(sum, number, index){ +// return sum + (number * (Math.pow(10, index))); +// }); +// } module.exports = ArrayFunctions; diff --git a/starter-code/src/bubble-sort.js b/starter-code/src/bubble-sort.js index fe84337..ad32975 100644 --- a/starter-code/src/bubble-sort.js +++ b/starter-code/src/bubble-sort.js @@ -1,12 +1,12 @@ function bubbleSort(array) { - var length = array.length; - for (var i = (length - 1); i > 0; i--) { + const length = array.length; + for (let i = (length - 1); i > 0; i--) { // Number of passes - for (var j = (length - i); j > 0; j--) { + for (let j = (length - i); j > 0; j--) { // Compare the adjacent positions if (array[j] < array[j - 1]) { // Swap the numbers - var tmp = array[j]; + const tmp = array[j]; array[j] = array[j - 1]; array[j - 1] = tmp; } diff --git a/starter-code/src/merge-sort.js b/starter-code/src/merge-sort.js index f6e4e70..e1348dc 100644 --- a/starter-code/src/merge-sort.js +++ b/starter-code/src/merge-sort.js @@ -1,15 +1,15 @@ function mergeSort(array) { if(array.length < 2) { return array } - var middle = Math.floor(array.length / 2); - var left = array.slice(0, middle); - var right = array.slice(middle); + const middle = Math.floor(array.length / 2); + const left = array.slice(0, middle); + const right = array.slice(middle); return sortHalves(mergeSort(left), mergeSort(right)); } function sortHalves(left, right) { - var array = []; + let array = []; while(left.length && right.length) { if(left[0] < right[0]) { @@ -21,7 +21,7 @@ function sortHalves(left, right) { // array.slice() with no arguments is a trick to make a copy of the array // .concat is to smash all of the arrays together // ...maybe there's an ES6 way to do this? - return array.concat(left.slice()).concat(right.slice()); + return [...array, ...left, ...right]; } module.exports = mergeSort; diff --git a/starter-code/src/sequencer.js b/starter-code/src/sequencer.js index 02284cc..ff13a21 100644 --- a/starter-code/src/sequencer.js +++ b/starter-code/src/sequencer.js @@ -1,68 +1,66 @@ -var LetterSequence = function(){} - -LetterSequence.prototype.createSequence = function(sequence){ - var characters = sequence.split(""); - var containerString = ""; - var repeatCount = 1; - - for (var i = 0; i < characters.length; i++){ - var currentChar = characters[i]; - var prevChar = characters[i - 1]; - var nextChar = characters[i + 1]; - - if (currentChar === prevChar){ - repeatCount++ - } - - // If the sequence is broken, and the repeat count is greater than 1 - // add the letter and the repeat count to the return string - if (currentChar !== nextChar && repeatCount >= 1){ - var repeats = repeatCount > 1 ? String(repeatCount) : "" - containerString += (repeats + currentChar) - repeatCount = 1; - } +class LetterSequence { + constructor() { } - return containerString; -} - -LetterSequence.prototype.decodeSequence = function(sequence){ - var containerString = ""; - var characters = sequence.split(""); - - for (var i = 0; i < characters.length; i++){ - var current = characters[i]; - var nextChar = characters[i + 1]; - - // If the current character is not a number, then there must be a letter after it - if (!isNaN(characters[i])){ - // So repeat it n times, and add it to our return value - var letters = this._repeat(current, nextChar); - containerString += letters; - // If the current character is a letter, and the last character is a letter, then - // it must be a lone letter - } else if (isNaN(characters[i]) && isNaN(characters[i - 1])){ - containerString += characters[i] + createSequence(sequence) { + let characters = sequence.split(""); + let containerString = ""; + let repeatCount = 1; + + for (let i = 0; i < characters.length; i++){ + const currentChar = characters[i]; + const prevChar = characters[i - 1]; + const nextChar = characters[i + 1]; + + if (currentChar === prevChar){ + repeatCount++ + } + if (currentChar !== nextChar && repeatCount >= 1){ + let repeats = repeatCount > 1 ? String(repeatCount) : "" + containerString += (repeats + currentChar) + repeatCount = 1; + } } + return containerString; } - return containerString; -} - -// Maybe there's a function to do this in ES6...? - -LetterSequence.prototype._repeat = function(count, character){ - var characters = ""; - - if (count <= 1){ - count = 1 + decodeSequence(sequence) { + let containerString = ""; + let characters = sequence.split(""); + + for (let i = 0; i < characters.length; i++){ + const current = characters[i]; + const nextChar = characters[i + 1]; + + // If the current character is not a number, then there must be a letter after it + if (!isNaN(characters[i])){ + // So repeat it n times, and add it to our return value + const letters = this._repeat(current, nextChar); + containerString += letters; + // If the current character is a letter, and the last character is a letter, then + // it must be a lone letter + } else if (isNaN(characters[i]) && isNaN(characters[i - 1])){ + containerString += characters[i] + } + } + + return containerString; } - for (var i = 0; i < count; i++){ - characters += character; + _repeat(count, character) { + let characters = ""; + + if (count <= 1){ + count = 1 + } + + for (let i = 0; i < count; i++){ + characters += character; + } + + return characters; } - return characters; } module.exports = LetterSequence;