From 87e64c625601bb6deb0b816bd287e8d12731aee0 Mon Sep 17 00:00:00 2001 From: Scott Martin Date: Mon, 5 Feb 2018 16:22:06 +0000 Subject: [PATCH 1/9] First two exercises --- .gitignore | 1 + src/index.js | 42 +++++++++++++++++++++++++++++++++++++++++- test/index.test.js | 18 ++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 07e6e47..5e52727 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules +package-lock.json diff --git a/src/index.js b/src/index.js index 47844e2..17ab2e9 100644 --- a/src/index.js +++ b/src/index.js @@ -2,4 +2,44 @@ function add(a, b){ return a + b; } -exports.add = add; +function longestString(stringsArray) { + let seenLengths = {}; + + let lengths = stringsArray.map(string => { + seenLengths[string.length] + ? seenLengths[string.length] += 1 + : seenLengths[string.length] = 1; + return string.length; + }); + + let maxLength = Math.max(...lengths); + let maxLengthIndex = lengths.indexOf(maxLength); + + if (seenLengths[maxLength] > 1) return -1; + + return stringsArray[maxLengthIndex]; +} + +function l337 (inputString) { + let dictionary = { + i : 1, + l : 1, + z : 2, + e : 3, + a : 4, + s : 5, + g : 6, + t : 7, + y : 7, + b : 8, + q : 9, + o : '0' + }; + + // fail if no input + return inputString.split('').map(x => dictionary[x] || x ).join(''); +} + +module.exports.add = add; +module.exports.longestString = longestString; +module.exports.l337 = l337; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index b1f6772..2dda9a5 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -5,3 +5,21 @@ test('Addition', function(){ const result = functions.add(2, 3); expect(result).toBe(expected); }); + +test('Identify the longest string in an array', function (){ + const expected = 'encyclopedia'; + const result = functions.longestString(['hat', 'fish', 'abacus', 'encyclopedia' ]); + expect(result).toBe(expected); +}); + +test('Return an error value where there are two equally longest strings in an array', function (){ + const expected = -1; + const result = functions.longestString(['hat', 'fish', 'abacus', 'encyclopedia', 'encyclopedit' ]); + expect(result).toBe(expected); +}); + +test('Turn a string into leetspeak', function () { + const expected = '0m6 7h15 15 50 1337 101'; + const result = functions.l337('omg this is so leet lol'); + expect(result).toBe(expected); +}); From 77361586992e4007f57024c00750c92d4e739f21 Mon Sep 17 00:00:00 2001 From: Scott Martin Date: Mon, 5 Feb 2018 17:21:11 +0000 Subject: [PATCH 2/9] Next five exercises --- src/index.js | 20 ++++++++++++++++++-- test/index.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 17ab2e9..c94c520 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,8 @@ function longestString(stringsArray) { } function l337 (inputString) { + //if (!inputString) throw new Error ('No input specified'); + let dictionary = { i : 1, l : 1, @@ -36,10 +38,24 @@ function l337 (inputString) { o : '0' }; - // fail if no input return inputString.split('').map(x => dictionary[x] || x ).join(''); } +function uniqueStrings (stringArray) { + return stringArray.reduce((acc,string)=>{ + if(acc.includes(string)) return acc + acc.push(string) + return acc + },[]); +} + +function Developer (name, languages) { + this.name = name; + this.languages = languages; +} + module.exports.add = add; module.exports.longestString = longestString; -module.exports.l337 = l337; \ No newline at end of file +module.exports.l337 = l337; +module.exports.uniqueStrings = uniqueStrings; +module.exports.Developer = Developer; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 2dda9a5..39f677a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -18,8 +18,34 @@ test('Return an error value where there are two equally longest strings in an ar expect(result).toBe(expected); }); +// test('Turning a string into leetspeak requires input', function () { +// //const result = ; +// expect(functions.l337()).toThrow(); +// }); + test('Turn a string into leetspeak', function () { const expected = '0m6 7h15 15 50 1337 101'; const result = functions.l337('omg this is so leet lol'); expect(result).toBe(expected); }); + +test('Return unique strings in an array', function() { + const expected = ['hat', 'fish', 'abacus', 'encyclopedia']; + const result = functions.uniqueStrings (['hat', 'fish', 'abacus','fish', 'encyclopedia', 'hat']) + expect(result).toEqual(expected); +}); + +test('Developer constructor returns an instance of Developer', function() { + const result = new functions.Developer('Jane Code', [ 'Ada', 'COBOL', 'FORTH' ]); + expect(result).toBeInstanceOf(functions.Developer); +}); + +test('Developer constructor returns a named developer with languages', function() { + const expected = { + name : 'Jane Code', + languages : [ 'Ada', 'COBOL', 'FORTH' ] + }; + const result = new functions.Developer('Jane Code', [ 'Ada', 'COBOL', 'FORTH' ]); + expect(result).toEqual(expected); +}); + From 38ddb498c599098cc30f4145c5f85de91c4df018 Mon Sep 17 00:00:00 2001 From: Scott Martin Date: Mon, 5 Feb 2018 17:21:53 +0000 Subject: [PATCH 3/9] Whitespace --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index 39f677a..69fe325 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -31,7 +31,7 @@ test('Turn a string into leetspeak', function () { test('Return unique strings in an array', function() { const expected = ['hat', 'fish', 'abacus', 'encyclopedia']; - const result = functions.uniqueStrings (['hat', 'fish', 'abacus','fish', 'encyclopedia', 'hat']) + const result = functions.uniqueStrings (['hat', 'fish', 'abacus', 'fish', 'encyclopedia', 'hat']) expect(result).toEqual(expected); }); From 2b2f00fd22cb1ef4e778be84d3fc0e91c84c54ce Mon Sep 17 00:00:00 2001 From: Scott Martin Date: Mon, 5 Feb 2018 18:08:52 +0000 Subject: [PATCH 4/9] Next four exercises --- src/index.js | 29 +++++++++++++++++- test/index.test.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c94c520..66dfdc7 100644 --- a/src/index.js +++ b/src/index.js @@ -54,8 +54,35 @@ function Developer (name, languages) { this.languages = languages; } +Developer.prototype.learnLanguage = function (language) { + if (!this.languages.includes(language)) this.languages.push(language); +}; + +function Garden (plants) { + Object.assign(this,plants); +} + +Garden.prototype.plant = function (plants) { + Object.keys(plants).forEach( newPlant => { + this[newPlant] + ? this[newPlant] += plants[newPlant] + : this[newPlant] = plants[newPlant]; + }); +} + +Garden.prototype.harvest = function (plants) { + Object.keys(plants).forEach( harvesting => { + if(this[harvesting]){ + this[harvesting] -= plants[harvesting]; + if (this[harvesting]< 1) delete this[harvesting]; + } + }); +}; + + module.exports.add = add; module.exports.longestString = longestString; module.exports.l337 = l337; module.exports.uniqueStrings = uniqueStrings; -module.exports.Developer = Developer; \ No newline at end of file +module.exports.Developer = Developer; +module.exports.Garden = Garden; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 69fe325..5a30340 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -49,3 +49,77 @@ test('Developer constructor returns a named developer with languages', function( expect(result).toEqual(expected); }); +test('Developer can learn a language', function() { + let jane = new functions.Developer('Jane Code', [ 'Ada', 'COBOL', 'FORTH' ]); + jane.learnLanguage('PROLOG'); + const expected = { + name : 'Jane Code', + languages : [ 'Ada', 'COBOL', 'FORTH', 'PROLOG' ] + }; + expect(jane).toEqual(expected); +}); + +test('Garden constructors returns a garden with plants and quantities of each plant' , function(){ + const expected = { + dandelion : 5, + poppy : 6, + rose : 7, + tulip : 8 + } + const result = new functions.Garden({ + dandelion : 5, + poppy : 6, + rose : 7, + tulip : 8 + }); + expect(result).toEqual(expected); +}); + +test('Garden can receive more stock from an object', function() { + const expected = { + dandelion : 7, + poppy : 8, + posy : 4, + rose : 9, + tulip : 8 + }; + const result = new functions.Garden({ + dandelion : 5, + poppy : 6, + rose : 7, + tulip : 8 + }); + + result.plant({ + dandelion : 2, + poppy : 2, + posy : 4, + rose : 2 + }); + + expect(result).toEqual(expected); +}); + +test('Garden can be harvested to specification in an object', function() { + const expected = { + dandelion : 4, + poppy : 5, + tulip : 7 + }; + const result = new functions.Garden({ + dandelion : 5, + poppy : 6, + rose : 7, + tulip : 8 + }); + + result.harvest({ + dandelion : 1, + poppy : 1, + rose : 10, + tulip : 1, + daffodil : 1 + }); + + expect(result).toEqual(expected); +}); \ No newline at end of file From 32840e1eb9740ebb23716a577396f64cdab7830d Mon Sep 17 00:00:00 2001 From: CskCnklc Date: Tue, 6 Feb 2018 12:25:16 +0000 Subject: [PATCH 5/9] two more functions solved --- src/index.js | 17 ++++++++++++++++- test/index.test.js | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 66dfdc7..a8573b7 100644 --- a/src/index.js +++ b/src/index.js @@ -79,10 +79,25 @@ Garden.prototype.harvest = function (plants) { }); }; +stringsConcat= function(inputArray){ + return inputArray.reduce((finalString, input)=>{ + if( typeof input == 'string' ) finalString.push(input); + return finalString; + },[]).join(' ') +} + +function negativeOnly (inputArray) { + return inputArray.reduce((output, input) => { + if (input < 0) output.push(input); + return output; + }, []); +} module.exports.add = add; module.exports.longestString = longestString; module.exports.l337 = l337; module.exports.uniqueStrings = uniqueStrings; module.exports.Developer = Developer; -module.exports.Garden = Garden; \ No newline at end of file +module.exports.Garden = Garden; +module.exports.stringsConcat = stringsConcat; +module.exports.negativeOnly = negativeOnly; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 5a30340..8c64c30 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -122,4 +122,16 @@ test('Garden can be harvested to specification in an object', function() { }); expect(result).toEqual(expected); -}); \ No newline at end of file +}); + +test('Return concatenated strings from array of Strings and Numbers', function(){ + const expected= 'I am going home!'; + const result= functions.stringsConcat(['I','am',1,0,123,'going', 2, 'home!']); + expect(result).toBe(expected) +}) + +test('Return an array of negative values from an input array of values', function() { + const expected = [ -5, -4, -3, -2, -1 ]; + const result = functions.negativeOnly([ 5, -5, 4, -4, 3, -3, 2, -2, 1, -1]); + expect(result).toEqual(expected); +}) \ No newline at end of file From fe24562a4a2db318134d0cd38de7432ac5d3e38e Mon Sep 17 00:00:00 2001 From: CskCnklc Date: Tue, 6 Feb 2018 15:14:40 +0000 Subject: [PATCH 6/9] 3 more exercises --- src/index.js | 39 ++++++++++++++++++++++++++++++++++++++- test/index.test.js | 18 ++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a8573b7..2406593 100644 --- a/src/index.js +++ b/src/index.js @@ -93,11 +93,48 @@ function negativeOnly (inputArray) { }, []); } +function camelise (inputString) { + let inputArray = inputString.split(' '); + + let firstWord = inputArray.shift(); + let outputString = inputArray.map((word) =>{ + return word.slice(0,1).toUpperCase() + word.slice(1); + }).join(''); + return firstWord.slice(0,1).toLowerCase() + firstWord.slice(1) + outputString; +} + +function merging (objectArray){ + return objectArray.sort((x,y)=>{ + return Object.keys(y).length - Object.keys(x).length + }).reduce((acc,obj)=>{ + acc = Object.assign(acc,obj); + return acc; + },{}) + +} + +function possibleValues(objectArray) { + return objectArray.reduce((acc, obj) => { + Object.keys(obj).forEach((objKey) => { + if (acc[objKey] && !acc[objKey].includes(obj[objKey])) { + acc[objKey].push(obj[objKey]); + } else if (!acc[objKey]) { + acc[objKey] = [ obj[objKey] ]; + } + }); + + return acc; + }, {}); +} + module.exports.add = add; module.exports.longestString = longestString; module.exports.l337 = l337; +module.exports.merging = merging module.exports.uniqueStrings = uniqueStrings; module.exports.Developer = Developer; module.exports.Garden = Garden; module.exports.stringsConcat = stringsConcat; -module.exports.negativeOnly = negativeOnly; \ No newline at end of file +module.exports.negativeOnly = negativeOnly; +module.exports.camelise = camelise; +module.exports.possibleValues = possibleValues; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 8c64c30..d59f888 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -134,4 +134,22 @@ test('Return an array of negative values from an input array of values', functio const expected = [ -5, -4, -3, -2, -1 ]; const result = functions.negativeOnly([ 5, -5, 4, -4, 3, -3, 2, -2, 1, -1]); expect(result).toEqual(expected); +}) + +test('Convert a string of space-separated words into a camelCase string', function(){ + const expected = 'aCamelCaseString'; + const result = functions.camelise('A Camel Case String'); + expect(result).toBe(expected); +}) + +test('The objects with fewest values should take precedence over objects with more values', function(){ + const expected = {a: 5, b: 2, c:32} + const result = functions.merging([{a: 5}, {a: 3, b: 21, c:32}, {b: 2}]) + expect(result).toEqual(expected); +}) + +test('Turn an array of objects into an object containing arrays of unique values', function(){ + const expected = {a:[5,3], b:[21], c:[32]}; + const result = functions.possibleValues([{a: 5}, {a: 3, b: 21, c:32}, {a: 3, c:32}]); + expect(result).toEqual(expected); }) \ No newline at end of file From 21a3e49f599ed647dc606df6d93fc1327ed566ed Mon Sep 17 00:00:00 2001 From: CskCnklc Date: Tue, 6 Feb 2018 15:47:55 +0000 Subject: [PATCH 7/9] 2 more exercises --- src/index.js | 24 +++++++++++++++++++++++- test/index.test.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 2406593..336ea53 100644 --- a/src/index.js +++ b/src/index.js @@ -127,6 +127,26 @@ function possibleValues(objectArray) { }, {}); } +function isPrime(n){ + if(n<2)return false; + + let q= Math.floor(Math.sqrt(n)); + + for(let i =2; i<= q ; i++){ + if(n%i===0) return false; + } + return true; +} + +function Walker (direction='N') { + if (direction.match(/^[NSEW]$/i)) { + this.direction = direction; + } else { + this.direction = 'N'; + console.log(`I don't understand ${direction}, defaulting to N`); + } +} + module.exports.add = add; module.exports.longestString = longestString; module.exports.l337 = l337; @@ -137,4 +157,6 @@ module.exports.Garden = Garden; module.exports.stringsConcat = stringsConcat; module.exports.negativeOnly = negativeOnly; module.exports.camelise = camelise; -module.exports.possibleValues = possibleValues; \ No newline at end of file +module.exports.possibleValues = possibleValues; +module.exports.isPrime= isPrime +module.exports.Walker = Walker; \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index d59f888..e389f45 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -152,4 +152,37 @@ test('Turn an array of objects into an object containing arrays of unique values const expected = {a:[5,3], b:[21], c:[32]}; const result = functions.possibleValues([{a: 5}, {a: 3, b: 21, c:32}, {a: 3, c:32}]); expect(result).toEqual(expected); -}) \ No newline at end of file +}) + +test('Check if the number is prime or not', function(){ + const prime= functions.isPrime(982451653); + expect(prime).toBe(true); + + const notPrime= functions.isPrime(982451658); + expect(notPrime).toBe(false); +}) + +test('Walker constructor with no direction returns a Walker facing N', function() { + const expected = { + direction : 'N' + }; + const result = new functions.Walker(); + expect(result).toEqual(expected); +}); + +test('Walker constructor with direction E returns a Walker facing E', function() { + const expected = { + direction : 'E' + }; + const result = new functions.Walker('E'); + expect(result).toEqual(expected); +}); + + +test('Walker constructor with bad direction returns a Walker facing N', function() { + const expected = { + direction : 'N' + }; + const result = new functions.Walker('Dave'); + expect(result).toEqual(expected); +}); \ No newline at end of file From 32aa78d5e79f2b7540f1be33e854de79506bae8e Mon Sep 17 00:00:00 2001 From: CskCnklc Date: Tue, 6 Feb 2018 15:51:02 +0000 Subject: [PATCH 8/9] Walker objects need to contain coordinates --- src/index.js | 2 ++ test/index.test.js | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 336ea53..0d5cf61 100644 --- a/src/index.js +++ b/src/index.js @@ -145,6 +145,8 @@ function Walker (direction='N') { this.direction = 'N'; console.log(`I don't understand ${direction}, defaulting to N`); } + + this.coords = [0,0]; } module.exports.add = add; diff --git a/test/index.test.js b/test/index.test.js index e389f45..e24db26 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -164,7 +164,8 @@ test('Check if the number is prime or not', function(){ test('Walker constructor with no direction returns a Walker facing N', function() { const expected = { - direction : 'N' + direction : 'N', + coords : [0,0] }; const result = new functions.Walker(); expect(result).toEqual(expected); @@ -172,7 +173,8 @@ test('Walker constructor with no direction returns a Walker facing N', function( test('Walker constructor with direction E returns a Walker facing E', function() { const expected = { - direction : 'E' + direction : 'E', + coords : [0,0] }; const result = new functions.Walker('E'); expect(result).toEqual(expected); @@ -181,7 +183,8 @@ test('Walker constructor with direction E returns a Walker facing E', function() test('Walker constructor with bad direction returns a Walker facing N', function() { const expected = { - direction : 'N' + direction : 'N', + coords : [0,0] }; const result = new functions.Walker('Dave'); expect(result).toEqual(expected); From 7b62786519926ca992e3fc3e8c72cceac5d6e1a0 Mon Sep 17 00:00:00 2001 From: CskCnklc Date: Tue, 6 Feb 2018 16:31:46 +0000 Subject: [PATCH 9/9] All solutions --- src/index.js | 26 ++++++++++++++++++++++++++ test/index.test.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 0d5cf61..02fe289 100644 --- a/src/index.js +++ b/src/index.js @@ -147,6 +147,32 @@ function Walker (direction='N') { } this.coords = [0,0]; + this.pathTaken = [[0,0]]; +} + +Walker.prototype.walk = function (direction,steps){ + + direction= direction.toUpperCase(); + this.direction=direction; + + if(direction === 'N') { + this.coords[1] += steps; + }else if(direction === 'S') { + this.coords[1] -= steps; + } + else if(direction === 'E') { + this.coords[0] += steps; + } + else if(direction === 'W') { + this.coords[0] -= steps; + } + + this.pathTaken.push([...this.coords]) + +} + +Walker.prototype.pathTaken = function () { + return this.pathTaken; } module.exports.add = add; diff --git a/test/index.test.js b/test/index.test.js index e24db26..da58fbc 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -165,7 +165,8 @@ test('Check if the number is prime or not', function(){ test('Walker constructor with no direction returns a Walker facing N', function() { const expected = { direction : 'N', - coords : [0,0] + coords : [0,0], + pathTaken : [[0,0]] }; const result = new functions.Walker(); expect(result).toEqual(expected); @@ -174,7 +175,8 @@ test('Walker constructor with no direction returns a Walker facing N', function( test('Walker constructor with direction E returns a Walker facing E', function() { const expected = { direction : 'E', - coords : [0,0] + coords : [0,0], + pathTaken : [[0,0]] }; const result = new functions.Walker('E'); expect(result).toEqual(expected); @@ -184,8 +186,31 @@ test('Walker constructor with direction E returns a Walker facing E', function() test('Walker constructor with bad direction returns a Walker facing N', function() { const expected = { direction : 'N', - coords : [0,0] + coords : [0,0], + pathTaken : [[0,0]] }; const result = new functions.Walker('Dave'); expect(result).toEqual(expected); -}); \ No newline at end of file +}); + +test('Walker walks to direction specified as many steps as given', function(){ + const expected = { + direction : 'N', + coords : [0,5], + pathTaken: [[0,0], [0,5]] + } + const walker = new functions.Walker('N'); + walker.walk('N',5); + expect(walker).toEqual(expected); +}) + +test('Output the journey history of a Walker as a list of coordinates', function() { + const walker = new functions.Walker('N'); + walker.walk('N', 5); + walker.walk('E', 2); + walker.walk('N', 3); + walker.walk('W', 7); + + const expected = [ [0,0], [0,5], [2,5], [2,8], [-5,8] ]; + expect(walker.pathTaken).toEqual(expected); +}) \ No newline at end of file