diff --git a/exercism/diamond/diamond.js b/exercism/diamond/diamond.js index c8de2a8..4853650 100644 --- a/exercism/diamond/diamond.js +++ b/exercism/diamond/diamond.js @@ -2,46 +2,46 @@ var Diamond = function() {}; Diamond.prototype.makeDiamond = function(input) { let letter = input.toUpperCase() -let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -let diamondLimit = [] -let diamondArray = [] - -for (let i = 0; i < alphabet.length; i++) { - diamondLimit.push(alphabet[i]) - if (alphabet[i] === letter) { - break; + let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + let diamondLimit = alphabet.slice(0, alphabet.indexOf(letter)+1).split('') + let diamondArray = [] + + // for (let i = 0; i < alphabet.length; i++) { + // diamondLimit.push(alphabet[i]) + // if (alphabet[i] === letter) { + // break; + // } + // } + + let diamondSize = diamondLimit.length * 2 - 1 + + for (let i = 0; i < diamondSize; i++) { + diamondArray.push(Array(diamondSize).fill(' ')) + } + let leftLetterPlacement = diamondLimit.length - 1 + let rightLetterPlacement = diamondLimit.length - 1 + + let i = 0 + let j = 0 + while (i < diamondArray.length / 2) { + diamondArray[i][rightLetterPlacement] = diamondLimit[j] + diamondArray[i][leftLetterPlacement] = diamondLimit[j] + rightLetterPlacement-- + leftLetterPlacement++ + j++ + i++ + } + + j = diamondLimit.length - 2 + while (i < diamondArray.length) { + diamondArray[i] = diamondArray[j] + j-- + i++ + } + + for ( let i = 0; i < diamondArray.length; i++) { + diamondArray[i] = diamondArray[i].join('') } -} - -let diamondSize = diamondLimit.length * 2 - 1 - -for (let i = 0; i < diamondSize; i++) { - diamondArray.push(Array(diamondSize).fill(' ')) -} -let leftLetterPlacement = diamondLimit.length - 1 -let rightLetterPlacement = diamondLimit.length - 1 - -let i = 0 -let j = 0 -while (i < diamondArray.length/2) { - diamondArray[i][rightLetterPlacement] = diamondLimit[j] - diamondArray[i][leftLetterPlacement] = diamondLimit[j] - rightLetterPlacement-- - leftLetterPlacement++ - j++ - i++ -} - -j = diamondLimit.length - 2 -while (i < diamondArray.length) { - diamondArray[i] = diamondArray[j] - j-- - i++ -} - -for ( let i = 0; i < diamondArray.length; i++) { - diamondArray[i] = diamondArray[i].join('') -} return diamondArray.join('\n') + "\n" }; diff --git a/exercism/hamming/hamming.js b/exercism/hamming/hamming.js index e36c05a..c7cce5e 100644 --- a/exercism/hamming/hamming.js +++ b/exercism/hamming/hamming.js @@ -1,23 +1,14 @@ var Hamming = function() {}; Hamming.prototype.checkForSameLength = function(firstStrand, secondStrand) { - if (firstStrand.length === secondStrand.length) { - return true; - } - return false; + return firstStrand.length === secondStrand.length ? true : false } Hamming.prototype.compute = function(firstStrand, secondStrand) { if (this.checkForSameLength(firstStrand, secondStrand)) { - let firstArray = firstStrand.split(''); - let secondArray = secondStrand.split(''); - let hammingDistance = 0 - for (var i = 0; i < firstArray.length; i++) { - if (firstArray[i] !== secondArray[i]) { - hammingDistance++ - } - } - return hammingDistance + return firstStrand.split('').reduce( function(accumulator, current, index){ + return current !== secondStrand[index] ? accumulator + 1 : accumulator + }, 0) } else { throw new Error('DNA strands must be of equal length.') } diff --git a/exercism/kindergarten-garden/kindergarten-garden.js b/exercism/kindergarten-garden/kindergarten-garden.js new file mode 100644 index 0000000..63aee28 --- /dev/null +++ b/exercism/kindergarten-garden/kindergarten-garden.js @@ -0,0 +1,50 @@ +var Garden = function(diagram, students=[ + 'Alice','Bob','Charlie','David', + 'Eve','Fred', 'Ginny', 'Harriet', + 'Ileana', 'Joseph', 'Kincaid', 'Larry' +]) { + this.diagram = diagram.split('\n') + this.students = students.sort() + + var splitRowOne = this.diagram[0].split('') + var splitRowTwo = this.diagram[1].split('') + var plantPairsRowOne = [] + var plantPairsRowTwo = [] + var plants = { + C: "clover", + G: "grass", + V: "violets", + R: "radishes" + } + + while(splitRowOne.length > 0) { + plantPairsRowOne.push(splitRowOne.splice(0,2)) + } + + while(splitRowTwo.length > 0) { + plantPairsRowTwo.push(splitRowTwo.splice(0,2)) + } + + // make an array that combines the index values in plantPairsRowOne & plantPairsRowTwo + var plantLettersAssigned = [] + for (var i = 0; i < plantPairsRowOne.length; i++) { + plantLettersAssigned.push(plantPairsRowOne[i].concat(plantPairsRowTwo[i])) + } + + var plantAssignments = plantLettersAssigned.map(function(element) { + return element.map(function(letter){ + return plants[letter] + }) + }) + + //loop through this.students, create this[students[i]] = [plantAssignments[i]] + i = 0 + while (i < this.students.length) { + this[students[i].toLowerCase()] = plantAssignments[i] + i++ + } + +}; + + +module.exports = Garden; diff --git a/exercism/kindergarten-garden/kindergarten-garden.spec.js b/exercism/kindergarten-garden/kindergarten-garden.spec.js index dcc9c7c..78fc9aa 100644 --- a/exercism/kindergarten-garden/kindergarten-garden.spec.js +++ b/exercism/kindergarten-garden/kindergarten-garden.spec.js @@ -7,17 +7,17 @@ describe('Garden', function () { .toEqual(['radishes', 'clover', 'grass', 'grass']); }); - xit('another for Alice', function () { + it('another for Alice', function () { expect(new Garden('VC\nRC').alice) .toEqual(['violets', 'clover', 'radishes', 'clover']); }); - xit('for Bob', function () { + it('for Bob', function () { expect(new Garden('VVCG\nVVRC').bob) .toEqual(['clover', 'grass', 'radishes', 'clover']); }); - xit('for Bob and Charlie', function () { + it('for Bob and Charlie', function () { var garden = new Garden('VVCCGG\nVVCCGG'); expect(garden.bob).toEqual(['clover', 'clover', 'clover', 'clover']); expect(garden.charlie).toEqual(['grass', 'grass', 'grass', 'grass']); @@ -29,62 +29,62 @@ describe('Full garden', function () { var diagram = 'VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV'; var garden = new Garden(diagram); - xit('for Alice', function () { + it('for Alice', function () { expect(garden.alice) .toEqual(['violets', 'radishes', 'violets', 'radishes']); }); - xit('for Bob', function () { + it('for Bob', function () { expect(garden.bob) .toEqual(['clover', 'grass', 'clover', 'clover']); }); - xit('for Charlie', function () { + it('for Charlie', function () { expect(garden.charlie) .toEqual(['violets', 'violets', 'clover', 'grass']); }); - xit('for David', function () { + it('for David', function () { expect(garden.david) .toEqual(['radishes', 'violets', 'clover', 'radishes']); }); - xit('for Eve', function () { + it('for Eve', function () { expect(garden.eve) .toEqual(['clover', 'grass', 'radishes', 'grass']); }); - xit('for Fred', function () { + it('for Fred', function () { expect(garden.fred) .toEqual(['grass', 'clover', 'violets', 'clover']); }); - xit('for Ginny', function () { + it('for Ginny', function () { expect(garden.ginny) .toEqual(['clover', 'grass', 'grass', 'clover']); }); - xit('for Harriet', function () { + it('for Harriet', function () { expect(garden.harriet) .toEqual(['violets', 'radishes', 'radishes', 'violets']); }); - xit('for Ileana', function () { + it('for Ileana', function () { expect(garden.ileana) .toEqual(['grass', 'clover', 'violets', 'clover']); }); - xit('for Joseph', function () { + it('for Joseph', function () { expect(garden.joseph) .toEqual(['violets', 'clover', 'violets', 'grass']); }); - xit('for Kincaid', function () { + it('for Kincaid', function () { expect(garden.kincaid) .toEqual(['grass', 'clover', 'clover', 'grass']); }); - xit('for Larry', function () { + it('for Larry', function () { expect(garden.larry) .toEqual(['grass', 'violets', 'clover', 'violets']); }); @@ -96,22 +96,22 @@ describe('Disordered class', function () { var students = ['Samantha', 'Patricia', 'Xander', 'Roger']; var garden = new Garden(diagram, students); - xit('Patricia', function () { + it('Patricia', function () { expect(garden.patricia) .toEqual(['violets', 'clover', 'radishes', 'violets']); }); - xit('Roger', function () { + it('Roger', function () { expect(garden.roger) .toEqual(['radishes', 'radishes', 'grass', 'clover']); }); - xit('Samantha', function () { + it('Samantha', function () { expect(garden.samantha) .toEqual(['grass', 'violets', 'clover', 'grass']); }); - xit('Xander', function () { + it('Xander', function () { expect(garden.xander) .toEqual(['radishes', 'grass', 'clover', 'violets']); }); @@ -123,7 +123,7 @@ describe('Two gardens, different students', function () { var garden1 = new Garden(diagram, ['Alice', 'Bob', 'Charlie', 'Dan']); var garden2 = new Garden(diagram, ['Bob', 'Charlie', 'Dan', 'Erin']); - xit('Bob and Charlie for each garden', function () { + it('Bob and Charlie for each garden', function () { expect(garden1.bob) .toEqual(['radishes', 'radishes', 'grass', 'clover']); expect(garden2.bob) diff --git a/exercism/largest-series-product/largest-series-product.js b/exercism/largest-series-product/largest-series-product.js new file mode 100644 index 0000000..b093555 --- /dev/null +++ b/exercism/largest-series-product/largest-series-product.js @@ -0,0 +1,30 @@ +var Series = function(inputNumber) { + if( /[^0-9]/g.test(inputNumber) ) { + throw new Error('Invalid input.') + } + + this.number = inputNumber +}; + +Series.prototype.largestProduct = function(seriesNumber) { + if( /[^0-9]/g.test(seriesNumber)) { + throw new Error('Invalid input.') + } + if (seriesNumber > this.number.length) { + throw new Error('Slice size is too big.') + } + + let largestProduct = 0 + for (let i = 0; i <= this.number.length-seriesNumber; i++) { + let currentSeries = this.number.slice(i, seriesNumber + i).split('') + let currentProduct = currentSeries.reduce(function(accumulator, next) { + return accumulator * next + }, 1) + if (currentProduct > largestProduct) { + largestProduct = currentProduct + } + } + return largestProduct +}; + +module.exports = Series; diff --git a/exercism/largest-series-product/largest-series-product.spec.js b/exercism/largest-series-product/largest-series-product.spec.js index 8c7e181..f7e2311 100644 --- a/exercism/largest-series-product/largest-series-product.spec.js +++ b/exercism/largest-series-product/largest-series-product.spec.js @@ -6,15 +6,15 @@ describe('Series', function () { expect(new Series('0123456789').largestProduct(2)).toBe(72); }); - xit('works for a tiny number', function () { + it('works for a tiny number', function () { expect(new Series('19').largestProduct(2)).toBe(9); }); - xit('can get the largest product of 3', function () { + it('can get the largest product of 3', function () { expect(new Series('1027839564').largestProduct(3)).toBe(270); }); - xit('can get the largest product of a big number', function () { + it('can get the largest product of a big number', function () { var largeNumber = '73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788' + '51843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648' + '95044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604' + @@ -27,41 +27,41 @@ describe('Series', function () { expect(new Series(largeNumber).largestProduct(13)).toBe(23514624000); }); - xit('returns 0 if all digits are zero', function () { + it('returns 0 if all digits are zero', function () { expect(new Series('0000').largestProduct(2)).toBe(0); }); - xit('returns 0 if all spans contain zero', function () { + it('returns 0 if all spans contain zero', function () { expect(new Series('99099').largestProduct(3)).toBe(0); }); - xit('rejects invalid character in input', ()=> { + it('rejects invalid character in input', ()=> { expect(function () { new Series('1234a5').largestProduct('2') }).toThrow(new Error('Invalid input.')); }); - xit('rejects negative span', function () { + it('rejects negative span', function () { expect(() => { new Series('12345').largestProduct(-1) }).toThrow(new Error('Invalid input.')); }); - xit('returns 1 for empty string and zero slice length', function () { + it('returns 1 for empty string and zero slice length', function () { expect(new Series('').largestProduct(0)).toBe(1); }); - xit('returns 1 for non-empty string and zero slice length', function () { + it('returns 1 for non-empty string and zero slice length', function () { expect(new Series('123').largestProduct(0)).toBe(1); }); - xit('throws an error for slices bigger than the number', function () { + it('throws an error for slices bigger than the number', function () { expect(function () { new Series('123').largestProduct(4); }).toThrow(new Error('Slice size is too big.')); }); - xit('throws an error for empty string and non-zero slice length', function () { + it('throws an error for empty string and non-zero slice length', function () { expect(function () { new Series('').largestProduct(1); }).toThrow(new Error('Slice size is too big.')); diff --git a/exercism/nth-prime/nth-prime.js b/exercism/nth-prime/nth-prime.js new file mode 100644 index 0000000..31ee1db --- /dev/null +++ b/exercism/nth-prime/nth-prime.js @@ -0,0 +1,34 @@ +var Prime = function() {}; + +Prime.prototype.isPrime = function(value) { + for(var i = 2; i < value; i++) { + if(value % i === 0) { + return false; + } + } + return value > 1; +} + +Prime.prototype.nth = function(givenNth) { + if (givenNth < 1) { + throw new Error('Prime is not possible') + } + //count up towards infinity + //check each number along the way + //for every prime number found, increase a counter + //once the counter hits givenNth stop the counting to infinity + //return the current number + let primeCounter = 0 + let currentNumber = 1 + while(primeCounter < givenNth) { + if (this.isPrime(currentNumber)) { + primeCounter += 1 + nthValue = currentNumber + } + currentNumber++ + } + + return nthValue +}; + +module.exports = Prime; diff --git a/exercism/nth-prime/nth-prime.spec.js b/exercism/nth-prime/nth-prime.spec.js index 840d5b8..e7d3ca7 100644 --- a/exercism/nth-prime/nth-prime.spec.js +++ b/exercism/nth-prime/nth-prime.spec.js @@ -1,4 +1,5 @@ -var prime = require ('./nth-prime'); +var Prime = require ('./nth-prime'); +var prime = new Prime() describe('Prime',function() { @@ -6,19 +7,19 @@ describe('Prime',function() { expect(prime.nth(1)).toEqual(2); }); - xit('second',function(){ + it('second',function(){ expect(prime.nth(2)).toEqual(3); }); - xit('sixth',function(){ + it('sixth',function(){ expect(prime.nth(6)).toEqual(13); }); - xit('big prime',function(){ + it('big prime',function(){ expect(prime.nth(10001)).toEqual(104743); }); - xit('weird case',function() { + it('weird case',function() { expect( function () { prime.nth(0); }).toThrow(new Error('Prime is not possible')); diff --git a/exercism/say/say.js b/exercism/say/say.js new file mode 100644 index 0000000..6267395 --- /dev/null +++ b/exercism/say/say.js @@ -0,0 +1,91 @@ +var Say = function() {}; + +const textConversion = { + 1: "one", + 2: "two", + 3: "three", + 4: "four", + 5: "five", + 6: "six", + 7: "seven", + 8: "eight", + 9: "nine", + 10: "ten", + 11: "eleven", + 12: "twelve", + 13: "thirteen", + 14: "fourteen", + 15: "fifteen", + 16: "sixteen", + 17: "seventeen", + 18: "eighteen", + 19: "nineteen", + 20: "twenty", + 30: "thirty", + 40: "forty", + 50: "fifty", + 60: "sixty", + 70: "seventy", + 80: "eighty", + 90: "ninety", + 100: "hundred", + 1000: "thousand", + 1000000: "million", + 1000000000: "billion" +}; + +Say.prototype.inEnglish = function(number) { + //inspired by: http://codereview.stackexchange.com/questions/124826/integer-to-english-challenge?noredirect=1&lq=1 + if (number < 0 || number > 999999999999) { + throw new Error('Number must be between 0 and 999,999,999,999.') + } + const numberArray = number.toString().split('').map(function(element) { + return parseInt(element); + }); + let translation = ''; + + if (number == 0) { + return 'zero' + } else if (number <= 20) { + translation = textConversion[number]; + } else if (numberArray.length < 3) { + //tens conversion + translation = [ + textConversion[numberArray[0]*10], + textConversion[numberArray[1]] + ].join('-'); + } else if (numberArray.length < 4) { + //hundreds conversion + if(number % 100 > 0) { + translation = [ + textConversion[numberArray[0]], + textConversion[100], + this.inEnglish(number % 100) + ].join(' '); + } else { + translation = [ + textConversion[numberArray[0]], + textConversion[100] + ].join(' '); + } + } else { + //higher than hundreds + const millOrBill = Math.pow(1000, Math.floor( (numberArray.length - 1) / 3) ) + if (number % millOrBill > 0) { + translation = [ + this.inEnglish(Math.floor(number / millOrBill)), + textConversion[millOrBill], + this.inEnglish(number % millOrBill) + ].join(' '); + } else { + translation = [ + this.inEnglish(Math.floor(number / millOrBill)), + textConversion[millOrBill] + ].join(' '); + } + } + + return translation.trim(); +}; + +module.exports = Say; diff --git a/exercism/say/say.spec.js b/exercism/say/say.spec.js index d638be8..25682c8 100644 --- a/exercism/say/say.spec.js +++ b/exercism/say/say.spec.js @@ -1,4 +1,5 @@ -var say = require('./say'); +var Say = require('./say'); +var say = new Say() describe('say', function () { @@ -6,56 +7,56 @@ describe('say', function () { expect(say.inEnglish(0)).toBe('zero'); }); - xit('one', function () { + it('one', function () { expect(say.inEnglish(1)).toBe('one'); }); - xit('fourteen', function () { + it('fourteen', function () { expect(say.inEnglish(14)).toBe('fourteen'); }); - xit('twenty', function () { + it('twenty', function () { expect(say.inEnglish(20)).toBe('twenty'); }); - xit('twenty-two', function () { + it('twenty-two', function () { expect(say.inEnglish(22)).toBe('twenty-two'); }); - xit('one hundred', function () { + it('one hundred', function () { expect(say.inEnglish(100)).toBe('one hundred'); }); - xit('one hundred twenty-three', function () { + it('one hundred twenty-three', function () { expect(say.inEnglish(123)).toBe('one hundred twenty-three'); }); - xit('one thousand', function () { + it('one thousand', function () { expect(say.inEnglish(1000)).toBe('one thousand'); }); - xit('one thousand two hundred thirty-four', function () { + it('one thousand two hundred thirty-four', function () { expect(say.inEnglish(1234)).toBe('one thousand two hundred thirty-four'); }); - xit('one million', function () { + it('one million', function () { expect(say.inEnglish(1000000)).toBe('one million'); }); - xit('one million two', function () { + it('one million two', function () { expect(say.inEnglish(1000002)).toBe('one million two'); }); - xit('one million two thousand three hundred forty-five', function () { + it('one million two thousand three hundred forty-five', function () { expect(say.inEnglish(1002345)) .toBe('one million two thousand three hundred forty-five'); }); - xit('one billion', function () { + it('one billion', function () { expect(say.inEnglish(1000000000)).toBe('one billion'); }); - xit('a really big number', function () { + it('a really big number', function () { var expected = 'nine hundred eighty-seven billion '; expected += 'six hundred fifty-four million '; expected += 'three hundred twenty-one thousand '; @@ -63,16 +64,16 @@ describe('say', function () { expect(say.inEnglish(987654321123)).toBe(expected); }); - xit('raises an error below zero', function () { + it('raises an error below zero', function () { expect(function () { say.inEnglish(-1); }).toThrow(new Error('Number must be between 0 and 999,999,999,999.')); }); - xit('raises an error above 999,999,999,999', function () { + it('raises an error above 999,999,999,999', function () { expect(function () { say.inEnglish(1000000000000); }).toThrow(new Error('Number must be between 0 and 999,999,999,999.')); }); -}); \ No newline at end of file +}); diff --git a/team_practice.md b/team_practice.md index 46877df..b5bbb4f 100644 --- a/team_practice.md +++ b/team_practice.md @@ -36,7 +36,7 @@ Exercism provides a number of practice problems along with unit tests to ensure - [x] Solve `/exercism/secret-handshake` - [ ] Solve `/exercism/wordy` -- [ ] Solve `/exercism/largest-series-product` +- [x] Solve `/exercism/largest-series-product` - [ ] Solve `/exercism/robot-simulator` - [ ] Solve `/exercism/rna-transcription` - [x] Solve `/exercism/bob` @@ -54,9 +54,9 @@ Exercism provides a number of practice problems along with unit tests to ensure ### Day 3 - Exercism - [ ] Solve `/exercism/kindergarten-garden` -- [ ] Solve `/exercism/nth-prime` +- [x] Solve `/exercism/nth-prime` - [ ] Solve `/exercism/palindrome-products` -- [ ] Solve `/exercism/say` +- [x] Solve `/exercism/say` - [ ] Solve `/exercism/queen-attack` #### Stretch