From aa1b7c930531062dee5c9334be004f94e42dd8b9 Mon Sep 17 00:00:00 2001 From: yuyinchen Date: Fri, 1 Jul 2022 15:04:42 +0800 Subject: [PATCH] update gilded rose --- js-jest/src/gilded_rose.js | 102 ++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 48746965..4b36f08d 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -10,53 +10,73 @@ class Shop { constructor(items=[]){ this.items = items; } - 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; + + get minQuality() { + return 0; + } + + get maxQuality() { + return 50; + } + + checkQuality({quality}) { + const {minQuality, maxQuality} = this; + + return quality - 1 < minQuality || quality + 1 > maxQuality; + } + + updateItemQuality(item) { + if (this.checkQuality(item)) { + return; + } + + if (item.sellIn < 0) { + if (item.name != 'Aged Brie') { + if (item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.quality - 1; } } } 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; - } - } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1; - } - } + item.quality = item.quality + 1; } } + if (item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.quality - 1; + } + } else { + if (item.name == 'Backstage passes to a TAFKAL80ETC concert') { + if (item.sellIn < 10 && item.sellIn > 5) { + item.quality = item.quality + 2; + } + else if (item.sellIn >= 0 && item.sellIn <= 5) { + item.quality = item.quality + 3; + } + else if (item.sellIn > 0) { + item.quality = item.quality + 1; + } else { + item.quality = 0; + } + } else { + item.quality = item.quality + 1; + } + } + } + + updateItemSellIn(item) { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.sellIn = item.sellIn - 1; + } + } + + updateQuality() { + this.items.forEach(item => { + this.updateItemSellIn(item); + this.updateItemQuality(item); + }); + return this.items; } }