Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions exercism/diamond/diamond.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Copy link

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?

// 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"
};
Expand Down
17 changes: 4 additions & 13 deletions exercism/hamming/hamming.js
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
Copy link

Choose a reason for hiding this comment

The 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){
Copy link

Choose a reason for hiding this comment

The 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.')
}
Expand Down
50 changes: 50 additions & 0 deletions exercism/kindergarten-garden/kindergarten-garden.js
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;
40 changes: 20 additions & 20 deletions exercism/kindergarten-garden/kindergarten-garden.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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']);
});
Expand All @@ -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']);
});
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions exercism/largest-series-product/largest-series-product.js
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++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you place a space in between this.number.length, seriesNumber, and the -?

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;
22 changes: 11 additions & 11 deletions exercism/largest-series-product/largest-series-product.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand All @@ -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.'));
Expand Down
Loading