tests not passing, code not returning what it should

This commit is contained in:
Ben Hemann 2021-06-07 20:45:17 -05:00
parent 0e11a9fa76
commit 2afc1921a7
2 changed files with 151 additions and 70 deletions

View File

@ -1,3 +1,5 @@
const maxQuality = 50;
const minQuality = 0;
// class to create items with name, sell by date, and quality
export class Item {
name: string;
@ -20,64 +22,143 @@ export class GildedRose {
// Method for updating item quality
updateQuality() {
var maxQuality = 50;
var minQuality = 0;
var conjured = 'Conjured';
var ragnaros = 'Sulfuras, Hand of Ragnaros';
var cheese = 'Aged Brie';
var concertPass = 'Backstage passes to a TAFKAL80ETC concert';
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name != cheese && this.items[i].name != concertPass
&& this.items[i].name != ragnaros && this.items[i].quality > minQuality) {
this.items[i].quality -=1;
} else {
if (this.items[i].quality < maxQuality) {
this.items[i].quality += 1;
if (this.items[i].name == concertPass) {
if (this.items[i].sellIn <= 10 && this.items[i].sellIn > 5) {
// quality goes up by 2 when days are 10 or less
if (this.items[i].quality < maxQuality) {
this.items[i].quality += 1;
}
}
// quality goes up by 3 when days are 5 of less
else if (this.items[i].sellIn <= 5) {
if (this.items[i].quality < maxQuality) {
this.items[i].quality += 2;
}
}
}
}
// for (let i = 0; i < this.items.length; i++) {
this.items.forEach(item => {
if(item.name == cheese){
cheesyFunction(item);
}
// checking for legendary to subtract sell by date
if (this.items[i].name != ragnaros) {
this.items[i].sellIn -= 1;
else if(item.name == ragnaros){
sulfurasHand(item);
}
if (this.items[i].sellIn < 0) {
if (this.items[i].name != cheese) {
if (this.items[i].name != concertPass) {
if (this.items[i].quality > minQuality) {
// move this if above line 57 if
if (this.items[i].name != ragnaros) {
this.items[i].quality -= 1;
}
}
// set quality to 0 after sellIn date passes
} else {
this.items[i].quality = 0;
}
// aged brie increases in quality here
} else if (this.items[i].name == cheese) {
if (this.items[i].quality < maxQuality) {
this.items[i].quality += 1;
}
}
else if(item.name == concertPass){
backstagePass(item);
}
}
return this.items;
else{
normalThing(item);
}
return item;
})
}
// if (this.items[i].name != cheese && this.items[i].name != concertPass
// && this.items[i].name != ragnaros && this.items[i].quality > minQuality) {
// this.items[i].quality -=1;
// } else {
// if (this.items[i].quality < maxQuality) {
// this.items[i].quality += 1;
// if (this.items[i].name == concertPass) {
// if (this.items[i].sellIn <= 10 && this.items[i].sellIn > 5) {
// // quality goes up by 2 when days are 10 or less
// if (this.items[i].quality < maxQuality) {
// this.items[i].quality += 1;
// }
// }
// // quality goes up by 3 when days are 5 of less
// else if (this.items[i].sellIn <= 5) {
// if (this.items[i].quality < maxQuality) {
// this.items[i].quality += 2;
// }
// }
// }
// }
// }
// // checking for legendary to subtract sell by date
// if (this.items[i].name != ragnaros) {
// this.items[i].sellIn -= 1;
// }
// if (this.items[i].sellIn < 0) {
// if (this.items[i].name != cheese) {
// if (this.items[i].name != concertPass) {
// if (this.items[i].quality > minQuality) {
// if (this.items[i].name != ragnaros) {
// this.items[i].quality -= 1;
// }
// }
// // set quality to 0 after sellIn date passes
// } else {
// this.items[i].quality = 0;
// }
// // aged brie increases in quality here
// } else if (this.items[i].name == cheese) {
// if (this.items[i].quality < maxQuality) {
// this.items[i].quality += 1;
// }
// }
//}
}// end class
function cheesyFunction(cheese){
var cheesy = {
name : cheese.name,
quality : cheese.quality,
sell : cheese.sellIn,
}
if(cheesy.quality < maxQuality){
cheesy.quality += 1;
cheesy.sell -= 1;
}
return cheesy;
}
function sulfurasHand(hand){
var sulfuras = {
name : hand.name,
quality : 80,
sell : hand.sellIn,
}
return sulfuras;
}
function backstagePass(concertPass){
const day1 = 10;
const day2 = 5;
var pass = {
name : concertPass.name,
quality : concertPass.quality,
sell : concertPass.sellIn,
}
if (pass.quality < maxQuality){
pass.quality += 1;
if (pass.sell <= day1 && pass.sell > day2)
pass.quality += 1;
pass.sell - 1;
}
else if(pass.sell <= day2){
pass.quality += 2;
pass.sell - 1;
}
return pass;
}
function normalThing(thing){
var item = {
name : thing.name,
quality : thing.quality,
sell : thing.sellIn,
}
item.sell -= 1;
item.quality -= 1;
if(item.sell <= 0){
item.sell -= 2;
}
if(item.quality <= 0){
item.quality = 0;
}
return item;
}

View File

@ -13,31 +13,31 @@ describe('Gilded Rose', () => {
});
it('Foo should be added to item array', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.name).to.equal('foo');
});
it('should give back updated sellIn ', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.sellIn).to.equal(4);
});
it('should give back updated quality', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(4);
});
it('Quality decreases twice as fast after sellIn date', () => {
gildedRose.items[0].sellIn = -1;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(3);
});
it('Quality should not go negative', () => {
gildedRose.items[0].quality = 0;
gildedRose.items[0].sellIn = -1;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(0);
});
});
@ -54,41 +54,41 @@ describe('Gilded Rose', () => {
});
it('Backstage passes to a TAFKAL80ETC concert should be added to item array', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.name).to.equal('Backstage passes to a TAFKAL80ETC concert');
});
it('should give back updated sellIn ', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.sellIn).to.equal(9);
});
it('should give back updated quality, Backstage passes increase in quality as days go on', () => {
gildedRose.items[0].sellIn = 12;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(6);
});
it('Backstage pass quality should increase by 2 with 10 or less days', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(7);
});
it('Backstage pass quality should increase by 3 with 5 or less days', () => {
gildedRose.items[0].sellIn = 2;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(8);
});
it('Backstage Pass quality drops to 0 after concert', () => {
gildedRose.items[0].sellIn = -1;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(0);
});
it('Backstage Pass quality caps at 50', () => {
gildedRose.items[0].quality = 50;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(50);
});
});
@ -105,29 +105,29 @@ describe('Gilded Rose', () => {
});
it('Aged Brie should be added to item array', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.name).to.equal('Aged Brie');
});
it('should give back updated sellIn ', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.sellIn).to.equal(6);
});
it('should give back updated quality, Aged Brie increases in quality as days go on', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(12);
});
it('Aged Brie quality caps at 50', () => {
gildedRose.items[0].quality = 50;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(50);
});
it('Aged Brie quality continues to go up even after sellIn date passes', () => {
gildedRose.items[0].sellIn = -12;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(13);
});
});
@ -144,23 +144,23 @@ describe('Gilded Rose', () => {
});
it('Sulfuras, Hand of Ragnaros should be added to item array', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.name).to.equal('Sulfuras, Hand of Ragnaros');
});
it('Sulfuras, Hand of Ragnaros, should not have an updated sellIn ', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.sellIn).to.equal(1);
});
it('Sulfuras, Hand of Ragnaros quality should not change', () => {
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(80);
});
it('Sulfuras, Hand of Ragnaros should not have degraded quality even after sellIn date passes', () => {
gildedRose.items[0].sellIn = -10;
const items = gildedRose.updateQuality()[0];
const items = gildedRose.items[0];
expect(items.quality).to.equal(80);
});
});