Cheating a bit ... and not technically an algo.
I tried the gilded rose refactoring kata again. If you want to have a go here is a repo with a starter pack in all different languages:
https://github.com/emilybache/GildedRose-Refactoring-Kata
The below is where I got to (with a pleasing amount of test coverage):
export class Item {
name: string;
sellIn: number;
quality: number;
constructor(name: string, sellIn: number, quality: number) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
}
export class Shop {
items: Item[];
constructor(items = []) {
this.items = items;
}
updateQuality() {
for (let i = 0; i < this.items.length; i++) {
const currentItem = this.items[i];
if (currentItem.name === 'Sulfuras, Hand of Ragnaros') {
continue;
}
if (currentItem.quality >= 50) {
currentItem.sellIn = currentItem.sellIn - 1;
continue;
}
if (
currentItem.name != 'Aged Brie' &&
currentItem.name != 'Backstage passes to a TAFKAL80ETC concert'
) {
//handle depreciating quality
if (currentItem.quality > 0) {
currentItem.quality = currentItem.quality - 1;
}
} else {
//handle increasing quality
currentItem.quality = currentItem.quality + 1;
if (currentItem.name == 'Backstage passes to a TAFKAL80ETC concert') {
this.handlePasses(currentItem);
}
}
currentItem.sellIn = currentItem.sellIn - 1;
//handle expired stuff
if (currentItem.sellIn < 0) {
if (currentItem.name === 'Aged Brie') {
currentItem.quality = currentItem.quality + 1;
return this.items;
}
if (currentItem.name === 'Backstage passes to a TAFKAL80ETC concert') {
currentItem.quality = currentItem.quality - currentItem.quality;
return this.items;
}
// check with first code if flipping them helped??! or has it changed how this works?
if (currentItem.quality > 0) {
currentItem.quality = currentItem.quality - 1;
}
}
}
return this.items;
}
private handlePasses(pass: Item) {
if (pass.sellIn < 11) {
pass.quality = pass.quality + 1;
}
if (pass.sellIn < 6) {
pass.quality = pass.quality + 1;
}
}
}
Struggling a bit with this challenge over the weekend!
Top comments (0)