diff --git a/index.js b/index.js index 525c9de1..d1ebc9af 100644 --- a/index.js +++ b/index.js @@ -8,15 +8,23 @@ const drugs = [ new Drug("Magic Pill", 15, 40) ]; const trial = new Pharmacy(drugs); - const log = []; for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) { log.push(JSON.stringify(trial.updateBenefitValue())); } +const stringLog = log.toString(); +let multipleLineLog = ""; +for(let i = 0; i < stringLog.length; i++){ + multipleLineLog += stringLog[i]; + if(stringLog[i]== ']') { + multipleLineLog += "\n"; + } +} + /* eslint-disable no-console */ -fs.writeFile("output.txt", log.toString(), err => { +fs.writeFile("output.txt", multipleLineLog, err => { if (err) { console.log("error"); } else { diff --git a/pharmacy.js b/pharmacy.js index cda44c41..4dbd5f56 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -12,55 +12,62 @@ export class Pharmacy { } updateBenefitValue() { for (var i = 0; i < this.drugs.length; i++) { - if ( - this.drugs[i].name != "Herbal Tea" && - this.drugs[i].name != "Fervex" - ) { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; - } - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - if (this.drugs[i].name == "Fervex") { - if (this.drugs[i].expiresIn < 11) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } - if (this.drugs[i].expiresIn < 6) { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } - } - } + const drugName = this.drugs[i].name; + if(drugName == "Magic Pill") { + continue; } - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1; - } - if (this.drugs[i].expiresIn < 0) { - if (this.drugs[i].name != "Herbal Tea") { - if (this.drugs[i].name != "Fervex") { - if (this.drugs[i].benefit > 0) { - if (this.drugs[i].name != "Magic Pill") { - this.drugs[i].benefit = this.drugs[i].benefit - 1; + // expiration evolution + this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1; + const expirationTime = this.drugs[i].expiresIn; + let evolutionValue = (expirationTime < 0)? 2:1; + let evolutionSign = -1; + let evolutionCoeff = 1; + let stillWorthCoeff = 1; + + switch (drugName) { + case 'Herbal Tea': + evolutionSign = 1; + break; + + case 'Fervex': + switch (true) { + case (expirationTime < 0): + stillWorthCoeff = 0; + break; + case (expirationTime < 6): + evolutionValue = 3; + break; + case (expirationTime < 11): + evolutionValue = 2; + break; + default: + break; } - } - } else { - this.drugs[i].benefit = - this.drugs[i].benefit - this.drugs[i].benefit; - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } + evolutionSign = 1; + break; + + case 'Dafalgan': + evolutionCoeff = 2; + break; + + default: + break; } - } + // calculate benefit evolution + this.drugs[i].benefit = (this.drugs[i].benefit + (evolutionValue * evolutionSign * evolutionCoeff)) * stillWorthCoeff; + + // general rules + // the benefit of an item is never more than 50 + if(this.drugs[i].benefit > 50) { + this.drugs[i].benefit = 50; + } + // the benefit of an item is never less than 0 + if(this.drugs[i].benefit < 0) { + this.drugs[i].benefit = 0; + } + } return this.drugs; } -} +} \ No newline at end of file diff --git a/pharmacy.test.js b/pharmacy.test.js index f0925fc1..b5c2ecc8 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -1,9 +1,112 @@ import { Drug, Pharmacy } from "./pharmacy"; describe("Pharmacy", () => { + // General rules + it("benefit can't go under 0", () => { + expect(new Pharmacy([new Drug("test", -1, 0)]).updateBenefitValue()).toEqual( + [new Drug("test", -2, 0)] + ); + }); + + it("benefit can't go above 50", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 10, 50)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", 9, 50)] + ); + }); + + + // General drugs it("should decrease the benefit and expiresIn", () => { expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual( [new Drug("test", 1, 2)] ); }); + + it("benefit degrades twice as fast when expiration date has passed", () => { + expect(new Pharmacy([new Drug("test", -1, 38)]).updateBenefitValue()).toEqual( + [new Drug("test", -2, 36)] + ); + }); + + + // Herbal Tea + it("Herbal Tea benefit increases instead of decreasing", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 4, 27)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", 3, 28)] + ); + }); + + it("Herbal Tea benefit increases twice as fast after the expiration date", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 0, 14)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", -1, 16)] + ); + }); + + + // Magic Pill + it("Magic Pill benefit and expiresIn never change", () => { + expect(new Pharmacy([new Drug("Magic Pill", 8, 22)]).updateBenefitValue()).toEqual( + [new Drug("Magic Pill", 8, 22)] + ); + }); + + + // Fervex + it("Fervex increases in benefit instead of decreasing", () => { + expect(new Pharmacy([new Drug("Fervex", 20, 17)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 19, 18)] + ); + }); + + it("Fervex increases by 2 when 5 < expiresIn < 10", () => { + expect(new Pharmacy([new Drug("Fervex", 8, 17)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 7, 19)] + ); + }); + + it("Fervex increases by 3 when 0 <= expiresIn < 6", () => { + expect(new Pharmacy([new Drug("Fervex", 4, 17)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 3, 20)] + ); + }); + + it("Fervex benefit goes to 0 if expiration date has passed", () => { + expect(new Pharmacy([new Drug("Fervex", 0, 17)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", -1, 0)] + ); + }); + + + // Dafalgan + it("Dafalgan degrades in benefit twice as fast as normal drugs", () => { + expect(new Pharmacy([new Drug("Dafalgan", 20, 45)]).updateBenefitValue()).toEqual( + [new Drug("Dafalgan", 19, 43)] + ); + }); + + + // Multiple Drugs at once + it("multiple drugs all update correctly", () => { + const inputDrugs = [ + new Drug("Doliprane", 20, 41), + new Drug("Herbal Tea", -2, 9), + new Drug("Fervex", 3, 28), + new Drug("Magic Pill", 12, 25), + new Drug("Dafalgan", 20, 39) + ]; + + const outputDrugs = [ + new Drug("Doliprane", 19, 40), + new Drug("Herbal Tea", -3, 11), + new Drug("Fervex", 2, 31), + new Drug("Magic Pill", 12, 25), + new Drug("Dafalgan", 19, 37) + ]; + + expect(new Pharmacy(inputDrugs).updateBenefitValue()).toEqual( + outputDrugs + ); + }); }); + +