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
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
env:
jest: true
node: true
parser: babel-eslint
parser: "@babel/eslint-parser"
extends:
- eslint:recommended
- plugin:prettier/recommended
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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("Magic Pill", 15, 40),
];
const trial = new Pharmacy(drugs);

Expand All @@ -17,7 +17,7 @@ for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) {
}

/* eslint-disable no-console */
fs.writeFile("output.txt", log, err => {
fs.writeFile("output.txt", log.toString(), (err) => {
if (err) {
console.log("error");
} else {
Expand Down
30 changes: 14 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
{
"name": "take-home-test",
"version": "1.0.0",
"version": "1.0.1",
"repository": "[email protected]:inato/take-home-test.git",
"author": "inato",
"license": "MIT",
"private": true,
"engines": {
"yarn": ">=1.7.0",
"node": "=12.x"
"yarn": ">=3.1.0",
"node": "=16.x"
},
"scripts": {
"lint": "eslint .",
"test": "jest",
"start": "babel-node index.js"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.6.0",
"eslint": "^5.6.0",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-prettier": "^2.6.2",
"jest": "24.8.0",
"prettier": "^1.14.3",
"regenerator-runtime": "^0.12.1"
"@babel/cli": "^7.16.7",
"@babel/core": "^7.16.7",
"@babel/eslint-parser": "^7.16.5",
"@babel/node": "^7.16.7",
"@babel/preset-env": "^7.16.7",
"babel-jest": "^27.4.6",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "27.4.7",
"prettier": "^2.5.1"
}
}
192 changes: 151 additions & 41 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
const minBenefitValue = 0;
const maxBenefitValue = 50;

// TODO : put this configuration object in BDD
const drugsConfiguration = {
"Herbal Tea": {
expireVariation: -1,
benefitVariation: [
{
operator: "lte",
value: 0,
benefitVariation: 2,
},
{
operator: "gt",
value: 0,
benefitVariation: 1,
},
],
},
"Magic Pill": {
expireVariation: 0,
benefitVariation: [],
},
Fervex: {
expireVariation: -1,
benefitVariation: [
{
operator: "lte",
value: 0,
benefitValue: 0,
},
{
operator: "lte",
value: 5,
benefitVariation: 3,
},
{
operator: "lte",
value: 10,
benefitVariation: 2,
},
],
},
Doliprane: {
expireVariation: -1,
benefitVariation: [
{
operator: "gt",
value: 0,
benefitVariation: -1,
},
{
operator: "lte",
value: 0,
benefitVariation: -2,
},
],
},
Dafalgan: {
expireVariation: -1,
benefitVariation: [
{
operator: "gt",
value: 0,
benefitVariation: -2,
},
{
operator: "lte",
value: 0,
benefitVariation: -4,
},
],
},
};

// Use for not configured drug like test in the pharmacy.test file
const defaultDrugConfiguration = {
expireVariation: -1,
benefitVariation: [
{
operator: "gt",
value: 0,
benefitVariation: -1,
},
{
operator: "lte",
value: 0,
benefitVariation: -2,
},
],
};

export class Drug {
constructor(name, expiresIn, benefit) {
this.name = name;
Expand All @@ -10,57 +103,74 @@ export class Pharmacy {
constructor(drugs = []) {
this.drugs = drugs;
}

/**
* Update the benefit value and the expiresIn of all drugs
* @return {*[]}
*/
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;
for (const drug of this.drugs) {
const tmpBenefit = this.getNewBenefitValue(drug);
const minBenefit = Math.min(tmpBenefit, maxBenefitValue);
drug.benefit = Math.max(minBenefit, minBenefitValue);
drug.expiresIn += drugsConfiguration[drug.name]
? drugsConfiguration[drug.name].expireVariation
: defaultDrugConfiguration.expireVariation;
}

return this.drugs;
}

/**
* Calculate the new possible value of the benefit of a drug
* @param drug [Drug]
* @return newBenefit [number]
*/
getNewBenefitValue(drug) {
let res = 0;
const conditions = drugsConfiguration[drug.name]
? drugsConfiguration[drug.name].benefitVariation
: defaultDrugConfiguration.benefitVariation;
if (conditions && conditions.length > 0) {
for (const condition of conditions) {
if (condition.operator === "gt") {
// Create 2 if for performances
if (drug.expiresIn > condition.value) {
if (typeof condition.benefitValue !== "undefined") {
return condition.benefitValue;
}
res = condition.benefitVariation;
break;
}
}
} 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;
}
// Create four conditions for possible futur needs
else if (condition.operator === "gte") {
if (drug.expiresIn >= condition.value) {
if (typeof condition.benefitValue !== "undefined") {
return condition.benefitValue;
}
res = condition.benefitVariation;
break;
}
}
}
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 if (condition.operator === "lt") {
if (drug.expiresIn < condition.value) {
if (typeof condition.benefitValue !== "undefined") {
return condition.benefitValue;
}
} else {
this.drugs[i].benefit =
this.drugs[i].benefit - this.drugs[i].benefit;
res = condition.benefitVariation;
break;
}
} else {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
} else if (condition.operator === "lte") {
if (drug.expiresIn <= condition.value) {
if (typeof condition.benefitValue !== "undefined") {
return condition.benefitValue;
}
res = condition.benefitVariation;
break;
}
}
}
}

return this.drugs;
return drug.benefit + res;
}
}
41 changes: 41 additions & 0 deletions pharmacy.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading