From 737704e19eed528c3b4725bd3754dcad91fa3e1c Mon Sep 17 00:00:00 2001 From: Arno Chauveau Date: Thu, 24 Jul 2025 09:29:15 +0200 Subject: [PATCH] Use Array.prototype.map over a for loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use Array.prototype.map over a for loop so that we don’t have to use array indexing all the time. --- TypeScript/app/gilded-rose.ts | 62 ++++++++++++++++------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/TypeScript/app/gilded-rose.ts b/TypeScript/app/gilded-rose.ts index ec735003..fe63cc89 100644 --- a/TypeScript/app/gilded-rose.ts +++ b/TypeScript/app/gilded-rose.ts @@ -27,60 +27,54 @@ export class GildedRose { } updateQuality() { - for (let i = 0; i < this.items.length; i++) { + return this.items.map((item) => { if ( - this.items[i].name !== "Aged Brie" && - this.items[i].name !== "Backstage passes to a TAFKAL80ETC concert" + item.name !== "Aged Brie" && + item.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; + if (item.quality > 0) { + 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 (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; } } - if (this.items[i].sellIn < 6) { - 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; } } } } } - if (this.items[i].name !== "Sulfuras, Hand of Ragnaros") { - this.items[i].sellIn = this.items[i].sellIn - 1; + if (item.name !== "Sulfuras, Hand of Ragnaros") { + item.sellIn = item.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; + 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 { - this.items[i].quality = - this.items[i].quality - this.items[i].quality; + item.quality = item.quality - item.quality; } } else { - if (this.items[i].quality < 50) { - this.items[i].quality = this.items[i].quality + 1; + if (item.quality < 50) { + item.quality = item.quality + 1; } } } - } - - return this.items; + return item; + }); } }