Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions TypeScript/app/gilded-rose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,52 @@ export class GildedRose {

updateQuality() {
for (let i = 0; i < this.items.length; i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider doing something like for (const item of this.items) and then referring to item so you don't have to keep repeating this.items[i]

if (this.items[i].name === 'Sulfuras, Hand of Ragnaros') {
if (this.items[i].name === "Sulfuras, Hand of Ragnaros") {
continue;
}

this.items[i].sellIn = this.items[i].sellIn - 1;

if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
if (this.items[i].sellIn < 0) {
this.items[i].quality = 0
continue;
}
switch (this.items[i].name) {
case "Backstage passes to a TAFKAL80ETC concert":
if (this.items[i].sellIn < 0) {
this.items[i].quality = 0;
break;
}

if (this.items[i].sellIn >= 10) {
this.items[i].quality = this.items[i].quality + 1
}
else if (this.items[i].sellIn >= 5) {
this.items[i].quality = this.items[i].quality + 2
}
else {
this.items[i].quality = this.items[i].quality + 3
}
if (this.items[i].sellIn >= 10) {
this.items[i].quality = this.items[i].quality + 1;
} else if (this.items[i].sellIn >= 5) {
this.items[i].quality = this.items[i].quality + 2;
} else {
this.items[i].quality = this.items[i].quality + 3;
}

if (this.items[i].quality > 50) {
this.items[i].quality = 50
}
continue;
}


if (this.items[i].name === 'Aged Brie') {
if (this.items[i].sellIn < 0) {
this.items[i].quality = Math.min(this.items[i].quality + 2, 50);
}
else {
this.items[i].quality = Math.min(this.items[i].quality + 1, 50);
}
continue;
}

if (this.items[i].sellIn < 0) {
this.items[i].quality = Math.max(0, this.items[i].quality - 2);
}
else {
this.items[i].quality = Math.max(0, this.items[i].quality - 1);
if (this.items[i].quality > 50) {
this.items[i].quality = 50;
}
break;
case "Aged Brie":
if (this.items[i].sellIn < 0) {
this.items[i].quality = Math.min(this.items[i].quality + 2, 50);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could pull out values like 50 here into well-named consts to increase clarity and reduce risk of typos

} else {
this.items[i].quality = Math.min(this.items[i].quality + 1, 50);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could create a helper function for clamping values so don't need to repeatedly use Math.min and Math.max throughout the switch statement

}
break;
case "Conjured Mana Cake":
if (this.items[i].sellIn < 0) {
this.items[i].quality = Math.max(0, this.items[i].quality - 4);
} else {
this.items[i].quality = Math.max(0, this.items[i].quality - 2);
}
break;
default:
if (this.items[i].sellIn < 0) {
this.items[i].quality = Math.max(0, this.items[i].quality - 2);
} else {
this.items[i].quality = Math.max(0, this.items[i].quality - 1);
}
break;
}
}
return this.items;
Expand Down
8 changes: 4 additions & 4 deletions TypeScript/test/jest/__snapshots__/approvals.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Sulfuras, Hand of Ragnaros, -1, 80
Backstage passes to a TAFKAL80ETC concert, 14, 21
Backstage passes to a TAFKAL80ETC concert, 9, 50
Backstage passes to a TAFKAL80ETC concert, 4, 50
Conjured Mana Cake, 2, 5
Conjured Mana Cake, 2, 4

-------- day 2 --------
name, sellIn, quality
Expand All @@ -46,7 +46,7 @@ Sulfuras, Hand of Ragnaros, -1, 80
Backstage passes to a TAFKAL80ETC concert, 13, 22
Backstage passes to a TAFKAL80ETC concert, 8, 50
Backstage passes to a TAFKAL80ETC concert, 3, 50
Conjured Mana Cake, 1, 4
Conjured Mana Cake, 1, 2

-------- day 3 --------
name, sellIn, quality
Expand All @@ -58,7 +58,7 @@ Sulfuras, Hand of Ragnaros, -1, 80
Backstage passes to a TAFKAL80ETC concert, 12, 23
Backstage passes to a TAFKAL80ETC concert, 7, 50
Backstage passes to a TAFKAL80ETC concert, 2, 50
Conjured Mana Cake, 0, 3
Conjured Mana Cake, 0, 0

-------- day 4 --------
name, sellIn, quality
Expand All @@ -70,7 +70,7 @@ Sulfuras, Hand of Ragnaros, -1, 80
Backstage passes to a TAFKAL80ETC concert, 11, 24
Backstage passes to a TAFKAL80ETC concert, 6, 50
Backstage passes to a TAFKAL80ETC concert, 1, 50
Conjured Mana Cake, -1, 1
Conjured Mana Cake, -1, 0

-------- day 5 --------
name, sellIn, quality
Expand Down
51 changes: 40 additions & 11 deletions TypeScript/test/jest/gilded-rose.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { Item, GildedRose } from '@/gilded-rose';

const POSSIBLE_ITEMS: String[] = ['Aged Brie', 'Sulfuras, Hand of Ragnaros', 'Backstage passes to a TAFKAL80ETC concert']

// describe('Gilded Rose', () => {
// it('should foo', () => {
// const gildedRose = new GildedRose([new Item('foo', 0, 0)]);
// const items = gildedRose.updateQuality();
// expect(items[0].name).toBe('foo');
// });
// });

type expectedValuesAfterUpdateSet = [name: string, sellIn: number, quality: number, expectedSellIn: number, expectedQuality: number];

it.each<expectedValuesAfterUpdateSet>([
Expand All @@ -18,7 +8,7 @@ it.each<expectedValuesAfterUpdateSet>([
['Base Case Item - at zero boundary', 1, 1, 0, 0], //at zero boundary
['Base Case Item - twice the speed', 0, 6, -1, 4], //twice the speed
['Base Case Item - sellIn goes negative', 0, 1, -1, 0], //sellIn goes negative
['Aged Brie', 15, 5, 14, 6], //Brie increases in
['Aged Brie', 15, 5, 14, 6], //Brie increases in
['Aged Brie', 0, 5, -1, 7], //Out of date brie twice as good
['Aged Brie', -9, 5, -10, 7], //Long out of date brie twice as good
['Aged Brie', -9, 50, -10, 50], //Long out of date brie twice as good
Expand All @@ -34,6 +24,9 @@ it.each<expectedValuesAfterUpdateSet>([
['Backstage passes to a TAFKAL80ETC concert', 5, 49, 4, 50], // more than 5 days but quality at max
['Backstage passes to a TAFKAL80ETC concert', 1, 49, 0, 50], // more than one day but quality at max
['Backstage passes to a TAFKAL80ETC concert', 0, 49, -1, 0], // day has passed but quality at max
['Conjured Mana Cake', 3, 6, 2, 4], //Conjured item standard speed
['Conjured Mana Cake', 0, 6, -1, 2], //Conjured item twice the speed
['Conjured Mana Cake', 0, 3, -1, 0], //Conjured item twice the speed but not below zero
])('should return %s %i %i %i %i', (name, sellIn, quality, expectedSellIn, expectedQuality) => {
const gildedRose = new GildedRose([new Item(name, sellIn, quality)]);
const items = gildedRose.updateQuality();
Expand Down Expand Up @@ -177,4 +170,40 @@ describe('updateQuality', () => {
expect(items[0].sellIn).toBe(-8);
expect(items[0].quality).toBe(50);
});

test('should decrease in sellIn and decrease quality by 2 if item is Conjured Mana Cake', () => {
const gildedRose = new GildedRose([new Item(
'Conjured Mana Cake',
3,
6
)]);
const items = gildedRose.updateQuality();
expect(items[0].name).toBe('Conjured Mana Cake');
expect(items[0].sellIn).toBe(2);
expect(items[0].quality).toBe(4);
});

test('should decrease in sellIn and decrease quality by 4 if SellIn negative and item is Conjured Mana Cake', () => {
const gildedRose = new GildedRose([new Item(
'Conjured Mana Cake',
-1,
6
)]);
const items = gildedRose.updateQuality();
expect(items[0].name).toBe('Conjured Mana Cake');
expect(items[0].sellIn).toBe(-2);
expect(items[0].quality).toBe(2);
});

test('should stop decrease when quality is 0 and item is Conjured Mana Cake', () => {
const gildedRose = new GildedRose([new Item(
'Conjured Mana Cake',
3,
0
)]);
const items = gildedRose.updateQuality();
expect(items[0].name).toBe('Conjured Mana Cake');
expect(items[0].sellIn).toBe(2);
expect(items[0].quality).toBe(0);
});
});