Last active
March 26, 2016 22:08
-
-
Save maciejlew/74db81244ff491dde85d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DoseComplexStrategy() { | |
var that = this; | |
var checkWeightIsInRange = function(weight) { | |
var ranges = that.getDoseParameters().getRanges(); | |
var min = ranges[0]; | |
var max = ranges[ranges.length - 1]; | |
if (!(weight >= min && weight <= max)) { | |
throw new RangeError('Weight out of range!'); | |
} | |
}; | |
this.checkDoseParameters = function(dose_parameters) { | |
if (!(dose_parameters instanceof DoseComplexParameters)) { | |
throw new TypeError('Wrong dose parameters!'); | |
} | |
}; | |
this.calculateDose = function(weight) { | |
this.checkWeightType(weight); | |
checkWeightIsInRange(weight); | |
var dose = new Dose(); | |
var ranges = this.getDoseParameters().getRanges(); | |
var doses = this.getDoseParameters().getDoses(); | |
var x1 = 0; | |
var x2 = ranges.length - 1; | |
var x = null; | |
while (x1 < x2) { | |
x = Math.floor((x1 + x2) / 2); | |
if (ranges[x] < weight) { | |
x1 = x + 1; | |
} else { | |
x2 = x; | |
} | |
} | |
if (ranges[x1] == weight) { | |
dose.setMin(doses[x1]); | |
dose.setMax(doses[x1]); | |
} else { | |
dose.setMin(0); | |
dose.setMax(0); | |
} | |
return dose; | |
}; | |
} | |
DoseComplexStrategy.prototype = new DoseStrategy(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment