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
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import fs from "fs";

const drugs = [
new Drug("Doliprane", 20, 30),
new Drug("Herbal Tea", 10, 5),
new Drug("Fervex", 5, 40),
new Drug("Magic Pill", 15, 40)
new Drug("Herbal Tea", 10, 5, 1),
new Drug("Fervex", 5, 40, 1, 1, { 10: 2, 5: 3, 0: -50 }),
new Drug("Magic Pill", 15, 40, 0, 0),
new Drug("Dafalgan", 20, 30, -2),
];
const trial = new Pharmacy(drugs);

Expand Down
70 changes: 22 additions & 48 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export class Drug {
constructor(name, expiresIn, benefit) {
constructor(name, expiresIn, benefit, benefitUpdateValue = -1, expirationUpdateValue = 1, benefitAcceleration = null) {
this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
this.benefitUpdateValue = benefitUpdateValue
this.expirationUpdateValue = expirationUpdateValue
this.benefitAcceleration = benefitAcceleration
}
}

Expand All @@ -11,56 +14,27 @@ export class Pharmacy {
this.drugs = drugs;
}
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;
}
}
}
}
return this.drugs.map((drug) => {
drug.expiresIn -= drug.expirationUpdateValue

if (drug.benefitAcceleration && Object.keys(drug.benefitAcceleration).includes(drug.expiresIn.toString())) {
drug.benefitUpdateValue = drug.benefitAcceleration[drug.expiresIn]
}
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1;

if (drug.expiresIn >= 0 && (drug.benefit > 0 && drug.benefit < 50)) {
// if drug has not expired and benefit is between 0 and 50, update benefit
drug.benefit += drug.benefitUpdateValue
}
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;
}
}
} 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;
}
}
else if (drug.expiresIn < 0 && (drug.benefit > 0 && drug.benefit < 50)) {
// if drug has expired and benefit is between 0 and 50, update benefit twice faster
drug.benefit += drug.benefitUpdateValue * 2
}
}

return this.drugs;
// if benefit has been updated but is not between 0 and 50, update benefit
if (drug.benefit < 0) drug.benefit = 0
if (drug.benefit > 50) drug.benefit = 50

return drug
})
}
}
53 changes: 48 additions & 5 deletions pharmacy.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
import { Drug, Pharmacy } from "./pharmacy";
import { Drug, Pharmacy } from './pharmacy';

describe("Pharmacy", () => {
it("should decrease the benefit and expiresIn", () => {
expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual(
[new Drug("test", 1, 2)]
describe('Pharmacy', () => {
it('should decrease the benefit and expiresIn', () => {
expect(new Pharmacy([new Drug('test', 2, 3)]).updateBenefitValue()).toEqual(
[new Drug('test', 1, 2)]
);
});

it('should change the benefit twice faster when expiresIn < 0', () => {
expect(new Pharmacy([new Drug('test', 0, 3)]).updateBenefitValue()).toEqual(
[new Drug('test', -1, 1)]
);
});

it('should never decrease the benefit below 0', () => {
expect(new Pharmacy([new Drug('test', 0, 0)]).updateBenefitValue()).toEqual(
[new Drug('test', -1, 0)]
);
});

it('should never increase the benefit above 50', () => {
expect(
new Pharmacy([new Drug('test', 2, 50)]).updateBenefitValue()
).toEqual([new Drug('test', 1, 50)]);
});

it('should increase the benefit value by benefitUpdateValue if parameter is set', () => {
expect(
new Pharmacy([new Drug('test', 5, 5, 2)]).updateBenefitValue()
).toEqual([new Drug('test', 4, 7, 2)]);
});

it('should change the benefitUpdateValue if benefitAcceleration parameter is set', () => {
expect(
new Pharmacy([
new Drug('Fervex', 7, 10, 1, 1, { 5: 2 }),
]).updateBenefitValue()
).toEqual([new Drug('Fervex', 6, 11, 1, 1, { 5: 2 })]);
expect(
new Pharmacy([
new Drug('Fervex', 6, 11, 1, 1, { 5: 2 }),
]).updateBenefitValue()
).toEqual([new Drug('Fervex', 5, 13, 2, 1, { 5: 2 })]);
});

it('should change the expiration by expirationUpdateValue if parameter is set', () => {
expect(
new Pharmacy([new Drug('test', 5, 5, -1, 0)]).updateBenefitValue()
).toEqual([new Drug('test', 5, 4, -1, 0)]);
});
});
Loading