From 3cca51abcf4381ca29f8d264e725795e2f20cb53 Mon Sep 17 00:00:00 2001 From: ismail-doitbig <89635383+ismail-doitbig@users.noreply.github.com> Date: Fri, 31 May 2024 11:16:42 +0200 Subject: [PATCH] Update gilded-rose.ts --- TypeScript/app/gilded-rose.ts | 84 +++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index db58d678..81eeac9b 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -10,6 +10,13 @@ export class Item { } } +enum ItemNames { + sulfuras = "Sulfuras, Hand of Ragnaros", + agedBrie = "Aged Brie", + backstagePasses = "Backstage passes to a TAFKAL80ETC concert", + conjuredManCake = "Conjured Mana Cake", +} + export class GildedRose { items: Array; @@ -19,49 +26,50 @@ export class GildedRose { 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 - } + let item = this.items[i]; + + if (item.name == ItemNames.sulfuras) { + item.quality = 80; + continue; + } + + // update sellIn logic + item.sellIn -= 1; + + // update quality + + if (item.name == ItemNames.conjuredManCake) { + item.quality -= 2; + if (item.sellIn < 0) { + item.quality -= 4; + } + } else if (item.name == ItemNames.agedBrie) { + item.quality += 1; + } else if (item.name == ItemNames.backstagePasses) { + if (item.sellIn > 10) { + item.quality += 1; + } else if (item.sellIn > 5 && item.sellIn <= 10) { + item.quality += 2; + } else if (item.sellIn >= 0 && item.sellIn <= 5) { + item.quality += 3; + } else if (item.sellIn < 0) { + item.quality = 0; } } 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].name != 'Sulfuras, Hand of Ragnaros') { - this.items[i].sellIn = this.items[i].sellIn - 1; - } - 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 - } - } - } else { - this.items[i].quality = this.items[i].quality - this.items[i].quality - } + // if the sellIn date has passed + if (item.sellIn < 0) { + item.quality -= 2; } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1 - } + // if it is within the sellin date + item.quality -= 1; } } + + // quality can never be negative + if (item.quality < 0) item.quality = 0; + + // has a ceiling of 50 + if (item.quality > 50) item.quality = 50; } return this.items;