From cde87568f541bd200601960f1e033b547a1419da Mon Sep 17 00:00:00 2001 From: Dan Holmes Date: Thu, 3 Dec 2020 14:17:22 +0000 Subject: [PATCH] Extract updating individual item --- js-jasmine/src/shop.js | 79 ++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/js-jasmine/src/shop.js b/js-jasmine/src/shop.js index 30094763..6f3762b7 100644 --- a/js-jasmine/src/shop.js +++ b/js-jasmine/src/shop.js @@ -4,52 +4,55 @@ class Shop { } updateQuality() { for (var 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; - } - } - } 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; - } - } - } + this._updateItemQuality(this.items[i]); + } + return this.items; + } + + _updateItemQuality(item) { + if (item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.quality > 0) { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.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 { + if (item.quality < 50) { + item.quality = item.quality + 1; + if (item.name == 'Backstage passes to a TAFKAL80ETC concert') { + if (item.sellIn < 11) { + if (item.quality < 50) { + item.quality = item.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; + if (item.sellIn < 6) { + if (item.quality < 50) { + item.quality = item.quality + 1; + } } } } } - - return this.items; + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.sellIn = item.sellIn - 1; + } + if (item.sellIn < 0) { + if (item.name != 'Aged Brie') { + if (item.name != 'Backstage passes to a TAFKAL80ETC concert') { + if (item.quality > 0) { + if (item.name != 'Sulfuras, Hand of Ragnaros') { + item.quality = item.quality - 1; + } + } + } else { + item.quality = item.quality - item.quality; + } + } else { + if (item.quality < 50) { + item.quality = item.quality + 1; + } + } + } } } module.exports = {