change for loop to forEach

This commit is contained in:
James Hall 2023-01-05 09:50:49 -06:00
parent 6a54ec762c
commit e363204d98

View File

@ -12,59 +12,50 @@ class Shop {
} }
updateQuality() { updateQuality() {
for (let i = 0; i < this.items.length; i++) { this.items.forEach(item => {
// Initialize three variables for easier reading below.
let itemSellIn = this.items[i].sellIn
let itemQuality = this.items[i].quality
let itemName = this.items[i].name
// Set a degradation multiplier to 2 if expiration date has passed // Set a degradation multiplier to 2 if expiration date has passed
// Otherwise set it to 1 (no multiplier) // Otherwise set it to 1 (no multiplier)
let degradationMultiplier = itemSellIn < 0 ? 2 : 1 let degradationMultiplier = item.sellIn < 0 ? 2 : 1
switch (itemName) { switch (item.name) {
case 'Aged Brie': case 'Aged Brie':
itemQuality++ item.quality++
itemSellIn-- item.sellIn--
break; break;
case 'Backstage passes to a TAFKAL80ETC concert': case 'Backstage passes to a TAFKAL80ETC concert':
switch (true) { switch (true) {
case (itemSellIn < 0): case (item.sellIn < 0):
itemQuality = 0 item.quality = 0
break; break;
case (itemSellIn <= 5): case (item.sellIn <= 5):
itemQuality += 3 item.quality += 3
break; break;
case (itemSellIn <= 10): case (item.sellIn <= 10):
itemQuality += 2 item.quality += 2
break; break;
default: default:
itemQuality++ item.quality++
break; break;
} }
itemSellIn-- item.sellIn--
break; break;
case 'Sulfuras, Hand of Ragnaros': case 'Sulfuras, Hand of Ragnaros':
break; break;
case 'Conjured Mana Cake': case 'Conjured Mana Cake':
itemQuality -= (2 * degradationMultiplier) item.quality -= (2 * degradationMultiplier)
itemSellIn-- item.sellIn--
break; break;
default: default:
itemQuality -= (1 * degradationMultiplier) item.quality -= (1 * degradationMultiplier)
itemSellIn-- item.sellIn--
} }
// Item quality cannot be higher than 50 or lower than 0. // Item quality cannot be higher than 50 or lower than 0.
if (itemQuality > 50) itemQuality = 50 if (item.quality > 50) item.quality = 50
if (itemQuality < 0) itemQuality = 0 if (item.quality < 0) item.quality = 0
// Use the modified variables to set the actual properties on the item })
this.items[i].sellIn = itemSellIn
this.items[i].quality = itemQuality
}
return this.items; return this.items;
} }