-
Notifications
You must be signed in to change notification settings - Fork 0
Weds exercism #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: TuesExercism
Are you sure you want to change the base?
Changes from all commits
c5f9d24
5c9fd03
d176f27
bbb7e4f
c069a1a
ad64380
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work in refactoring!! |
||
| } | ||
|
|
||
| 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){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am really liking your approach. Reduce is going to be with us for quite a while in JavaScript. |
||
| return current !== secondStrand[index] ? accumulator + 1 : accumulator | ||
| }, 0) | ||
| } else { | ||
| throw new Error('DNA strands must be of equal length.') | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you place a space in between |
||
| 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; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why you are keeping this commented code here?