From 6bc4443665610d6df0a60396e1800d38f378e25d Mon Sep 17 00:00:00 2001 From: katros Date: Mon, 20 Aug 2018 15:49:55 +0200 Subject: [PATCH] done --- .DS_Store | Bin 0 -> 6148 bytes package-lock.json | 3 + starter-code/package-lock.json | 320 ++++++++++++++++++++++++++++ starter-code/src/array-functions.js | 46 ++-- starter-code/src/bubble-sort.js | 8 +- starter-code/src/merge-sort.js | 35 +-- starter-code/src/sequencer.js | 98 ++++----- 7 files changed, 427 insertions(+), 83 deletions(-) create mode 100644 .DS_Store create mode 100644 package-lock.json create mode 100644 starter-code/package-lock.json diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6a5a1f5882db694d3d9ae8191e82de1c69ab6d42 GIT binary patch literal 6148 zcmeHKO>Wab6n@hh>KG6~LJCW7kXWQqq5x`zDw#B0bb*90f(4+~uC3I?^+dMQ2+|;L zxC0!4n{WdT!U5o$pCHAh5(|V-G*6oMy_tF6bLNXY;~^q8m_$9ICK1_a0xbvbHzwvq zyI^a2WCO@Vjs27#g((lw7X^(O1&jj!w*unbwWvcQN~o;8zmG6??}s$G2xpOp+!1BO zDW|x``BLNDqA>+@L~r3o6w@Z9;E8^RPN+bfEmvTzT7?EZqdj^|LwW-2&?!YV#>X1t zI_j2qUJf6_Z{m5JqWW)T-~W^sPK&YL*S?11G%3n%_ZwSVx2{~>ux-b7Za8oJV?OoE zpqvzgAb%x#PlNI})bVr9(_e8AzDVNfLG$)5&&nXl;#`uGC`QWbmq`}!>3~nNsE~Dh zHDTKgyK&HL&1Mg}ZMWa=&fD&6d#l%W@An_h=MB4cx3{x@c>3=B?0o*=6V?V}*q5c< z)VP4p2rkv>%+IrwXCve+_zRxF)loqIxAwKwS&~_aQNSqhmlP2DgNr86(^x5#TL+xH z0st)xOG7L_{ei9+0D2lLg&2V`MFlD<(^m|pZw^e+G4JX8N}-|?lTSt+{bZ(ZC`?~G zm@CtXc?yj;3K#{J61(-GThXX80pRE@bC(c?I?GR0v p$g31e3Y { + return number * 2; + }); + } + + superPower(array) { + return array.reduce((sum, number, index) => { + return sum + number * Math.pow(10, index); + }); + } +} // 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 +42,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..c8b3dfa 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]; + let 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..0bae205 100644 --- a/starter-code/src/merge-sort.js +++ b/starter-code/src/merge-sort.js @@ -1,27 +1,30 @@ function mergeSort(array) { - if(array.length < 2) { return 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); + let middle = Math.floor(array.length / 2); + let left = array.slice(0, middle); + let right = array.slice(middle); - return sortHalves(mergeSort(left), mergeSort(right)); + return sortHalves(mergeSort(left), mergeSort(right)); } function sortHalves(left, right) { - var array = []; + let array = []; - while(left.length && right.length) { - if(left[0] < right[0]) { - array.push(left.shift()); - } else { - array.push(right.shift()); + while (left.length && right.length) { + if (left[0] < right[0]) { + array.push(left.shift()); + } else { + array.push(right.shift()); + } } - } - // 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()); + // 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, ...left, ...right]; } module.exports = mergeSort; diff --git a/starter-code/src/sequencer.js b/starter-code/src/sequencer.js index 02284cc..b630ff4 100644 --- a/starter-code/src/sequencer.js +++ b/starter-code/src/sequencer.js @@ -1,68 +1,68 @@ -var LetterSequence = function(){} +var LetterSequence = function() {}; -LetterSequence.prototype.createSequence = function(sequence){ - var characters = sequence.split(""); - var containerString = ""; - var repeatCount = 1; +LetterSequence.prototype.createSequence = function(sequence) { + let characters = sequence.split(''); + let containerString = ''; + let repeatCount = 1; - for (var i = 0; i < characters.length; i++){ - var currentChar = characters[i]; - var prevChar = characters[i - 1]; - var nextChar = characters[i + 1]; + for (let i = 0; i < characters.length; i++) { + let currentChar = characters[i]; + let prevChar = characters[i - 1]; + let nextChar = characters[i + 1]; - if (currentChar === prevChar){ - repeatCount++ - } + 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; + // 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) { + let repeats = repeatCount > 1 ? String(repeatCount) : ''; + containerString += repeats + currentChar; + repeatCount = 1; + } } - } - return containerString; -} + return containerString; +}; -LetterSequence.prototype.decodeSequence = function(sequence){ - var containerString = ""; - var characters = sequence.split(""); +LetterSequence.prototype.decodeSequence = function(sequence) { + let containerString = ''; + let characters = sequence.split(''); - for (var i = 0; i < characters.length; i++){ - var current = characters[i]; - var nextChar = characters[i + 1]; + for (let i = 0; i < characters.length; i++) { + let current = characters[i]; + let 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] + // 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 + let 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; -} + return containerString; +}; // Maybe there's a function to do this in ES6...? -LetterSequence.prototype._repeat = function(count, character){ - var characters = ""; +LetterSequence.prototype._repeat = function(count, character) { + let characters = ''; - if (count <= 1){ - count = 1 - } + if (count <= 1) { + count = 1; + } - for (var i = 0; i < count; i++){ - characters += character; - } + for (let i = 0; i < count; i++) { + characters += character; + } - return characters; -} + return characters; +}; module.exports = LetterSequence;