Use Array.prototype.map over a for loop

Use Array.prototype.map over a for loop so that we don’t have to use 
array indexing all the time.
This commit is contained in:
Arno Chauveau 2025-07-24 09:29:15 +02:00
parent 6445f7f797
commit 737704e19e

View File

@ -27,60 +27,54 @@ export class GildedRose {
} }
updateQuality() { updateQuality() {
for (let i = 0; i < this.items.length; i++) { return this.items.map((item) => {
if ( if (
this.items[i].name !== "Aged Brie" && item.name !== "Aged Brie" &&
this.items[i].name !== "Backstage passes to a TAFKAL80ETC concert" item.name !== "Backstage passes to a TAFKAL80ETC concert"
) { ) {
if (this.items[i].quality > 0) { if (item.quality > 0) {
if (this.items[i].name !== "Sulfuras, Hand of Ragnaros") { if (item.name !== "Sulfuras, Hand of Ragnaros") {
this.items[i].quality = this.items[i].quality - 1; item.quality = item.quality - 1;
} }
} }
} else { } else {
if (this.items[i].quality < 50) { if (item.quality < 50) {
this.items[i].quality = this.items[i].quality + 1; item.quality = item.quality + 1;
if ( if (item.name === "Backstage passes to a TAFKAL80ETC concert") {
this.items[i].name === "Backstage passes to a TAFKAL80ETC concert" if (item.sellIn < 11) {
) { if (item.quality < 50) {
if (this.items[i].sellIn < 11) { item.quality = item.quality + 1;
if (this.items[i].quality < 50) {
this.items[i].quality = this.items[i].quality + 1;
} }
} }
if (this.items[i].sellIn < 6) { if (item.sellIn < 6) {
if (this.items[i].quality < 50) { if (item.quality < 50) {
this.items[i].quality = this.items[i].quality + 1; item.quality = item.quality + 1;
} }
} }
} }
} }
} }
if (this.items[i].name !== "Sulfuras, Hand of Ragnaros") { if (item.name !== "Sulfuras, Hand of Ragnaros") {
this.items[i].sellIn = this.items[i].sellIn - 1; item.sellIn = item.sellIn - 1;
} }
if (this.items[i].sellIn < 0) { if (item.sellIn < 0) {
if (this.items[i].name !== "Aged Brie") { if (item.name !== "Aged Brie") {
if ( if (item.name !== "Backstage passes to a TAFKAL80ETC concert") {
this.items[i].name !== "Backstage passes to a TAFKAL80ETC concert" if (item.quality > 0) {
) { if (item.name !== "Sulfuras, Hand of Ragnaros") {
if (this.items[i].quality > 0) { item.quality = item.quality - 1;
if (this.items[i].name !== "Sulfuras, Hand of Ragnaros") {
this.items[i].quality = this.items[i].quality - 1;
} }
} }
} else { } else {
this.items[i].quality = item.quality = item.quality - item.quality;
this.items[i].quality - this.items[i].quality;
} }
} else { } else {
if (this.items[i].quality < 50) { if (item.quality < 50) {
this.items[i].quality = this.items[i].quality + 1; item.quality = item.quality + 1;
} }
} }
} }
} return item;
});
return this.items;
} }
} }