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
51 changes: 51 additions & 0 deletions Drugs/drugsData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const drugsData = [
{
name: "Magic Pill",
noExpiration: true,
base: 0,
values: [
{
limit: 0,
value: 0
}
]
},
{
name: "Herbal Tea",
base: 1,
values: [
{
limit: 0,
value: 2
}
]
},
{
name: "Fervex",
base: 1,
hasToDrop: true,
values: [
{
limit: 10,
value: 2
},
{
limit: 5,
value: 3
}
]
},
{
name: "Dafalgan",
base: -1,
values: [{ limit: 0, value: -2 }]
}
];

const defaultDrugsData = {
name: "Default Drugs",
base: -1,
values: [{ limit: 0, value: -2 }]
};

export { drugsData, defaultDrugsData };
65 changes: 18 additions & 47 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { drugsData, defaultDrugsData } from "./Drugs/drugsData";

export class Drug {
constructor(name, expiresIn, benefit) {
this.name = name;
Expand All @@ -11,55 +13,24 @@ 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;
}
}
}
}
}
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;
}
}
} 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;
}
this.drugs.forEach(d => {
let drugData = drugsData.find(e => e.name == d.name);
if (!drugData) drugData = defaultDrugsData;
const { noExpiration, hasToDrop, base, values } = drugData;
if (!noExpiration) {
if (hasToDrop && d.expiresIn <= 0) d.benefit = 0;
else {
let newBase = values
.filter(v => d.expiresIn - v.limit <= 0)
.sort((a, b) => a.limit - b.limit)[0];
newBase = newBase ? newBase.value : base;
d.benefit += newBase;
if (d.benefit >= 50) d.benefit = 50;
else if (d.benefit <= 0) d.benefit = 0;
}
d.expiresIn--;
}
}
});

return this.drugs;
}
Expand Down
80 changes: 77 additions & 3 deletions pharmacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,82 @@ 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)]
);
expect(
new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("test", 1, 2)]);
});

it("should never the benefit be negative", () => {
expect(
new Pharmacy([new Drug("test", 2, -4)]).updateBenefitValue()
).toEqual([new Drug("test", 1, 0)]);
});

it("should never the benefit be more than 50", () => {
expect(
new Pharmacy([new Drug("test", 2, 59)]).updateBenefitValue()
).toEqual([new Drug("test", 1, 50)]);
});
});

describe("Herbal Tea", () => {
it("should increase benefit drug", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", 20, 20)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", 19, 21)]);
});

it("should increase benefit drug faster when expiration date is passed", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", -2, 20)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", -3, 22)]);
});
});

describe("Magic Pill", () => {
it("should never change", () => {
expect(
new Pharmacy([new Drug("Magic Pill", -20, -20)]).updateBenefitValue()
).toEqual([new Drug("Magic Pill", -20, -20)]);
});
});

describe("Fervex", () => {
it("should increase benefit drug by one when expiration date is more than 10", () => {
expect(
new Pharmacy([new Drug("Fervex", 17, 22)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 16, 23)]);
});

it("should increase benefit drug by two when expiration date is between than 10 and 5", () => {
expect(
new Pharmacy([new Drug("Fervex", 7, 22)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 6, 24)]);
});

it("should increase benefit drug by three when expiration date is less than 5", () => {
expect(
new Pharmacy([new Drug("Fervex", 4, 22)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 3, 25)]);
});

it("should drop benefit drug when expiration date is passed", () => {
expect(
new Pharmacy([new Drug("Fervex", -18, 22)]).updateBenefitValue()
).toEqual([new Drug("Fervex", -19, 0)]);
});
});

describe("Dafalgan", () => {
it("should decrease benefit drug", () => {
expect(
new Pharmacy([new Drug("Dafalgan", 20, 20)]).updateBenefitValue()
).toEqual([new Drug("Dafalgan", 19, 19)]);
});

it("should increase benefit drug faster when expiration date is passed", () => {
expect(
new Pharmacy([new Drug("Dafalgan", -2, 20)]).updateBenefitValue()
).toEqual([new Drug("Dafalgan", -3, 18)]);
});
});