feat: Implement "How many rats fit in a cage"
This commit is contained in:
parent
16d7c8f3dc
commit
5142bb66ca
107
src/index.js
107
src/index.js
@ -55,7 +55,7 @@ function bindLocaleSwitcher(initialValue) {
|
|||||||
i18next
|
i18next
|
||||||
.changeLanguage(e.target.value)
|
.changeLanguage(e.target.value)
|
||||||
.then(translatePageElements)
|
.then(translatePageElements)
|
||||||
.then(update);
|
.then(updateCageCheck);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ function bindLocaleSwitcher(initialValue) {
|
|||||||
await initI18next();
|
await initI18next();
|
||||||
translatePageElements();
|
translatePageElements();
|
||||||
bindLocaleSwitcher(i18next.resolvedLanguage);
|
bindLocaleSwitcher(i18next.resolvedLanguage);
|
||||||
update();
|
updateCageCheck();
|
||||||
})();
|
})();
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
@ -148,7 +148,20 @@ class Validator {
|
|||||||
if (numOfRats < 3 || numOfRats > 15) {
|
if (numOfRats < 3 || numOfRats > 15) {
|
||||||
throw new Error("This formula works only from 3 to 15 rats");
|
throw new Error("This formula works only from 3 to 15 rats");
|
||||||
}
|
}
|
||||||
return MINIMUM_AREA_THREE_RATS + (numOfRats - 3) * AREA_PER_ADDITIONAL_RAT;
|
return MINIMUM_AREA_THREE_RATS + (numOfRats - 3.0) * AREA_PER_ADDITIONAL_RAT;
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedNumberOfRats(overallArea) {
|
||||||
|
/*
|
||||||
|
Calculates the number of rats that are allowed for a certain overall area.
|
||||||
|
*/
|
||||||
|
let result = 3.0 + (overallArea-MINIMUM_AREA_THREE_RATS) / AREA_PER_ADDITIONAL_RAT;
|
||||||
|
console.log(`Area left: ${overallArea-MINIMUM_AREA_THREE_RATS}`);
|
||||||
|
console.log(`Result: ${result}`);
|
||||||
|
if (result < 3) {
|
||||||
|
throw new Error("Cages must be for three rats or more");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
failCageNumberIndependent(dimensions, numFullFloors) {
|
failCageNumberIndependent(dimensions, numFullFloors) {
|
||||||
@ -181,6 +194,11 @@ class Validator {
|
|||||||
return failedCriteria;
|
return failedCriteria;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOverallArea(dimensions, numFullFloors) {
|
||||||
|
const baseArea = dimensions.depth * dimensions.width;
|
||||||
|
return baseArea * numFullFloors
|
||||||
|
}
|
||||||
|
|
||||||
cageCheck(dimensions, numRats, numFullFloors) {
|
cageCheck(dimensions, numRats, numFullFloors) {
|
||||||
/*
|
/*
|
||||||
Function that checks a cage based on overall criteria and the number of rats.
|
Function that checks a cage based on overall criteria and the number of rats.
|
||||||
@ -191,9 +209,9 @@ class Validator {
|
|||||||
failedCriteria[FAILED_NUM_RATS] = this.FAIL_CRITERIA[FAILED_NUM_RATS];
|
failedCriteria[FAILED_NUM_RATS] = this.FAIL_CRITERIA[FAILED_NUM_RATS];
|
||||||
}
|
}
|
||||||
|
|
||||||
const areaNeeded = this.overallAreaNeeded(numRats);
|
const overallArea = this.getOverallArea(dimensions, numFullFloors);
|
||||||
const baseArea = dimensions.depth * dimensions.width;
|
const overallAreaNeeded = this.overallAreaNeeded(numRats);
|
||||||
if (baseArea * numFullFloors < areaNeeded) {
|
if (overallArea < overallAreaNeeded) {
|
||||||
failedCriteria[FAILED_OVERALL_AREA] = this.FAIL_CRITERIA[FAILED_OVERALL_AREA];
|
failedCriteria[FAILED_OVERALL_AREA] = this.FAIL_CRITERIA[FAILED_OVERALL_AREA];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,13 +243,30 @@ class Dimensions {
|
|||||||
// DOCUMENT INTERACTION //
|
// DOCUMENT INTERACTION //
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
|
|
||||||
const inputWidth = document.getElementById("width");
|
const numRatsCalculatorInputWidth = document.getElementById("num-rats-width");
|
||||||
inputWidth.onchange = updateViaManualMeasurements;
|
numRatsCalculatorInputWidth.onchange = updateNumRatsCalculator;
|
||||||
const inputDepth = document.getElementById("depth");
|
const numRatsCalculatorInputDepth = document.getElementById("num-rats-depth");
|
||||||
inputDepth.onchange = updateViaManualMeasurements;
|
numRatsCalculatorInputDepth.onchange = updateNumRatsCalculator;
|
||||||
const inputHeight = document.getElementById("height");
|
const numRatsCalculatorInputHeight = document.getElementById("num-rats-height");
|
||||||
inputHeight.onchange = updateViaManualMeasurements;
|
numRatsCalculatorInputHeight.onchange = updateNumRatsCalculator;
|
||||||
|
|
||||||
|
let numRatsNumFullFloors = document.getElementById("num-rats-numFullFloors");
|
||||||
|
numRatsNumFullFloors.oninput = function () {
|
||||||
|
updateNumRatsCalculator();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const inputWidth = document.getElementById("width");
|
||||||
|
inputWidth.onchange = updateCageCheck;
|
||||||
|
const inputDepth = document.getElementById("depth");
|
||||||
|
inputDepth.onchange = updateCageCheck;
|
||||||
|
const inputHeight = document.getElementById("height");
|
||||||
|
inputHeight.onchange = updateCageCheck;
|
||||||
|
|
||||||
|
let fullFloorNum = document.getElementById("numFullFloors");
|
||||||
|
fullFloorNum.oninput = function () {
|
||||||
|
updateCageCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let labelNumRats = document.getElementById("labelNumRats");
|
let labelNumRats = document.getElementById("labelNumRats");
|
||||||
@ -239,14 +274,9 @@ let labelNumRats = document.getElementById("labelNumRats");
|
|||||||
let ratSlider = document.getElementById("numRats");
|
let ratSlider = document.getElementById("numRats");
|
||||||
|
|
||||||
ratSlider.oninput = function () {
|
ratSlider.oninput = function () {
|
||||||
update();
|
updateCageCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Full floor functions
|
|
||||||
let fullFloorNum = document.getElementById("numFullFloors");
|
|
||||||
fullFloorNum.oninput = function () {
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getResultFromChecks(checks) {
|
function getResultFromChecks(checks) {
|
||||||
if (Object.keys(checks).length > 0) {
|
if (Object.keys(checks).length > 0) {
|
||||||
@ -265,11 +295,7 @@ function getResultFromChecks(checks) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateViaManualMeasurements() {
|
function updateCageCheck() {
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
function update() {
|
|
||||||
labelNumRats.innerHTML = i18next.t("cage-for-x-rats", {"num_rats": ratSlider.value});
|
labelNumRats.innerHTML = i18next.t("cage-for-x-rats", {"num_rats": ratSlider.value});
|
||||||
|
|
||||||
const width = inputWidth.value
|
const width = inputWidth.value
|
||||||
@ -285,3 +311,38 @@ function update() {
|
|||||||
resultsDiv.innerHTML = "";
|
resultsDiv.innerHTML = "";
|
||||||
resultsDiv.appendChild(result);
|
resultsDiv.appendChild(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateNumRatsCalculator() {
|
||||||
|
|
||||||
|
const width = numRatsCalculatorInputWidth.value
|
||||||
|
const depth = numRatsCalculatorInputDepth.value
|
||||||
|
const height = numRatsCalculatorInputHeight.value
|
||||||
|
const dimensions = new Dimensions(width / 100, depth / 100, height / 100);
|
||||||
|
const validator = new Validator();
|
||||||
|
const failed_checks = validator.failCageNumberIndependent(dimensions, fullFloorNum.value);
|
||||||
|
|
||||||
|
let overallArea = validator.getOverallArea(dimensions, numRatsNumFullFloors.value);
|
||||||
|
let allowedNumRats;
|
||||||
|
try {
|
||||||
|
allowedNumRats = validator.allowedNumberOfRats(overallArea);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
failed_checks[FAILED_BASE_AREA] = validator.FAIL_CRITERIA[FAILED_OVERALL_AREA];
|
||||||
|
allowedNumRats = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
console.log(`Allowed number: ${allowedNumRats}`);
|
||||||
|
|
||||||
|
let resultsDiv = document.getElementById("num-rats-resultsDiv");
|
||||||
|
|
||||||
|
const result = getResultFromChecks(failed_checks);
|
||||||
|
|
||||||
|
resultsDiv.innerHTML = "";
|
||||||
|
const p = document.createElement('p');
|
||||||
|
p.textContent = i18next.t("cage-for-x-rats", {"num_rats": allowedNumRats});
|
||||||
|
resultsDiv.appendChild(p);
|
||||||
|
resultsDiv.appendChild(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
const validator = new Validator();
|
Loading…
x
Reference in New Issue
Block a user