Update gilded-rose.ts: fixes and refactoring

- Added logic for handling conjured items.
- Improved flow of logic in the if else statements.
- Added code comments.
- Other minor refactoring changes.
This commit is contained in:
TeeMee123 2024-06-25 17:13:12 +01:00 committed by GitHub
parent e2abba77cb
commit 1fb9aaf8f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,48 +17,61 @@ export class GildedRose {
this.items = items;
}
isItemConjured(itemName: string): boolean {
return itemName.startsWith("Conjured");
}
updateQuality() {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
// Process quality increase exceptions
if (this.items[i].name == 'Aged Brie' || this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality <= 49) {
this.items[i].quality++;
// Process additional potential quality increases for backstage passes
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn <= 10 && this.items[i].quality <= 49) {
this.items[i].quality++;
}
if (this.items[i].sellIn <= 5 && this.items[i].quality <= 49) {
this.items[i].quality++;
}
}
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 11) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
if (this.items[i].sellIn < 6) {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
}
}
if (this.items[i].quality > 0) {
// Decrease quality except in certain cases
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1;
}
}
}
// Decrease 'sell in' value except in certain cases
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].sellIn = this.items[i].sellIn - 1;
let expiryRate = 1;
if(this.isItemConjured(this.items[i].name)) {
expiryRate = 2;
}
this.items[i].sellIn -= expiryRate;
}
// Process additional quality change due to expiry
if (this.items[i].sellIn < 0) {
if (this.items[i].name != 'Aged Brie') {
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].quality > 0) {
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
this.items[i].quality = this.items[i].quality - 1
this.items[i].quality--;
}
}
// Process some exceptional cases
} else {
this.items[i].quality = this.items[i].quality - this.items[i].quality
this.items[i].quality = 0;
}
} else {
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1
if (this.items[i].quality <= 49) {
this.items[i].quality++;
}
}
}