From 94cbe129e0f48325182654e22b9417fcb8c84f55 Mon Sep 17 00:00:00 2001 From: wengYuting Date: Thu, 7 Apr 2022 22:19:35 +0800 Subject: [PATCH] =?UTF-8?q?[=E9=87=8D=E6=A7=8B]=20=E7=B0=A1=E5=8C=96?= =?UTF-8?q?=E3=80=81=E7=B5=B1=E4=B8=80=E5=80=8B=E5=95=86=E5=93=81=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E9=82=8F=E8=BC=AF=E5=AF=AB=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js-jest/src/gilded_rose.js | 56 ++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/js-jest/src/gilded_rose.js b/js-jest/src/gilded_rose.js index 09227037..3a2b869d 100644 --- a/js-jest/src/gilded_rose.js +++ b/js-jest/src/gilded_rose.js @@ -16,7 +16,8 @@ class Shop { const item = this.items[i]; if (item.name === "Sulfuras, Hand of Ragnaros") continue; - item.sellIn = item.sellIn - 1; + item.sellIn -= 1; + switch (item.name) { case "Aged Brie": this.updateAgedBrie(item); @@ -33,39 +34,42 @@ class Shop { return this.items; } updateAgedBrie(item) { - if (item.quality < 50) { - item.quality = item.quality + 1; - - if (item.sellIn < 0) { - item.quality = item.quality + 1; - } - } - } - updateBackstagePasses(item) { - if (item.quality < 50) { - item.quality = item.quality + 1; - - if (item.sellIn < 10 && item.quality < 50) { - item.quality = item.quality + 1; - } - if (item.sellIn < 5 && item.quality < 50) { - item.quality = item.quality + 1; - } - } + let quality = item.quality; if (item.sellIn < 0) { - item.quality = item.quality - item.quality; + quality += 2; + } else { + quality += 1; } + + item.quality = quality < 50 ? quality : 50; + } + updateBackstagePasses(item) { + let quality = item.quality; + + if (item.sellIn < 0) { + quality = 0; + } else if (item.sellIn < 5) { + quality += 3; + } else if (item.sellIn < 10) { + quality += 2; + } else { + quality += 1; + } + + item.quality = quality < 50 ? quality : 50; } updateTheOthers(item) { - if (item.quality > 0) { - item.quality = item.quality - 1; + let quality = item.quality; - if (item.sellIn < 0) { - item.quality = item.quality - 1; - } + if (item.sellIn < 0) { + quality -= 2; + } else { + quality -= 1; } + + item.quality = quality > 0 ? quality : 0; } }