Hi everyone, it's been a while since I have messed around with JavaScript so this might be simple for someone who is able to help 🙂. I'm currently working on a calculator which seems to work fine for the first bit of the code regarding the compound elements, however with the variables under blend var bPrice = parseFloat(objectInputs["blend-price"]); the calculation I have for these elements are doubling but should be half of the compound elements , I have included the current script below as this was created from another developer so any help would be much appreciated 🙂
JavaScript:
"use strict";
function roundToFixed(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return (Math.round(value * multiplier) / multiplier).toFixed(precision);
}
var calculatorCompoundvsblended = {
nameCalculator: "compoundvsblended",
result: function result(objectInputs) {
// Inputs
var cPrice = parseFloat(objectInputs["compound-price"]);
var cnContent = parseFloat(objectInputs["compound-n-content"]);
var cpContent = parseFloat(objectInputs["compound-p-content"]);
var ckContent = parseFloat(objectInputs["compound-k-content"]);
var csContent = parseFloat(objectInputs["compound-s-content"]);
var bPrice = parseFloat(objectInputs["blend-price"]);
var bnContent = parseFloat(objectInputs["blend-n-content"]);
var bpContent = parseFloat(objectInputs["blend-p-content"]);
var bkContent = parseFloat(objectInputs["blend-k-content"]);
var bsContent = parseFloat(objectInputs["blend-s-content"]);
var cropYield = parseFloat(objectInputs["crop-yield-cvsb"]);
var cropPrice = parseFloat(objectInputs["crop-price-cvsb"]);
var cropNRate = parseFloat(objectInputs["crop-n-rate-cvsb"]);
var cropLoss = parseFloat(objectInputs["blend-loss"]);
// Calculations
// Outputs
var cFertiliserAmount = cropNRate / (cnContent / 50);
var cFertiliserAmountOutput = {
nameOutput: "amount-of-compound-fertiliser",
value: roundToFixed(cFertiliserAmount, 0),
};
var cFertiliserCost = cFertiliserAmount * cPrice / 100; //changed from 1000 to 100
var cFertiliserCostOutput = {
nameOutput: "cost-of-compound-fertiliser",
value: roundToFixed(cFertiliserCost, 0),
};
var cNRate = cFertiliserAmount * cnContent / 50; //changed to 50
var cNRateOutput = {
nameOutput: "compound-n-rate-applied",
value: roundToFixed(cNRate, 0),
};
var cPRate = cFertiliserAmount * cpContent / 50; //changed to 50
var cPRateOutput = {
nameOutput: "compound-p-rate-applied",
value: roundToFixed(cPRate, 0),
};
var cKRate = cFertiliserAmount * ckContent / 50; //changed to 50
var cKRateOutput = {
nameOutput: "compound-k-rate-applied",
value: roundToFixed(cKRate, 0),
};
var cSRate = cFertiliserAmount * csContent / 50; //changed to 50
var cSRateOutput = {
nameOutput: "compound-s-rate-applied",
value: roundToFixed(cSRate, 0),
};
var cExpectedYield = cropYield;
var cExpectedYieldOutput = {
nameOutput: "compound-expected-yield",
value: roundToFixed(cExpectedYield, 1)
};
var cLoss = 0;
var cLossOutput = {
nameOutput: "compound-yield-loss",
value: roundToFixed(cLoss, 1),
};
var cActualYield = cExpectedYield - cLoss;
var cActualYieldOutput = {
nameOutput: "compound-actual-yield",
value: roundToFixed(cActualYield, 1),
};
var cCropValue = cropPrice * cActualYield;
var cCropValueOutput = {
nameOutput: "compound-crop-value",
value: roundToFixed(cCropValue, 0),
};
var cMargin = cCropValue - cFertiliserCost;
var cMarginOutput = {
nameOutput: "compound-margin",
value: roundToFixed(cMargin, 0),
};
var bFertiliserAmount = cropNRate / (bnContent / 50); //changed from 100 to 50
var bFertiliserAmountOutput = {
nameOutput: "amount-of-blend-fertiliser",
value: roundToFixed(bFertiliserAmount, 0),
};
var bFertiliserCost = bFertiliserAmount * bPrice / 100; //changed from 1000 to 100
var bFertiliserCostOutput = {
nameOutput: "cost-of-blend-fertiliser",
value: roundToFixed(bFertiliserCost, 0),
};
var bNRate = bFertiliserAmount * bnContent / 50; //changed to 50 from 100
var bNRateOutput = {
nameOutput: "blend-n-rate-applied",
value: roundToFixed(bNRate, 0),
};
var bPRate = bFertiliserAmount * bpContent / 50; //changed to 50 from 100
var bPRateOutput = {
nameOutput: "blend-p-rate-applied",
value: roundToFixed(bPRate, 0),
};
var bKRate = bFertiliserAmount * bkContent / 50; //changed to 50 from 100
var bKRateOutput = {
nameOutput: "blend-k-rate-applied",
value: roundToFixed(bKRate, 0),
};
var bSRate = bFertiliserAmount * bsContent / 50; //changed to 50 from 100
var bSRateOutput = {
nameOutput: "blend-s-rate-applied",
value: roundToFixed(bSRate, 0),
};
var bExpectedYield = cropYield;
var bExpectedYieldOutput = {
nameOutput: "blend-expected-yield",
value: roundToFixed(bExpectedYield, 1)
};
var bLoss = bExpectedYield * cropLoss / 50; //changed to 50 from 100
var bLossOutput = {
nameOutput: "blend-yield-loss",
value: roundToFixed(bLoss, 1),
};
var bActualYield = bExpectedYield - bLoss;
var bActualYieldOutput = {
nameOutput: "blend-actual-yield",
value: roundToFixed(bActualYield, 1),
};
var bCropValue = cropPrice * bActualYield;
var bCropValueOutput = {
nameOutput: "blend-crop-value",
value: roundToFixed(bCropValue, 0),
};
var bMargin = bCropValue - bFertiliserCost;
var bMarginOutput = {
nameOutput: "blend-margin",
value: roundToFixed(bMargin, 0),
};
var comparison = (-1000 * (cMargin - bCropValue)) / bFertiliserAmount;
var comparisonOutput = {
nameOutput: "compund-blend-equivalent-price",
value: roundToFixed(comparison, 0),
};
var originalBestValue = cMargin >= bMargin;
var bestValue = {};
if (originalBestValue) {
bestValue.nameOutput = 'compound-output';
bestValue.style = 'success';
bestValue.titleName = 'card-title-one';
} else {
bestValue.nameOutput = 'blend-output';
bestValue.style = 'success';
bestValue.titleName = 'card-title-one';
}
return [
cFertiliserAmountOutput,
cFertiliserCostOutput,
cNRateOutput,
cPRateOutput,
cKRateOutput,
cSRateOutput,
cExpectedYieldOutput,
cLossOutput,
cActualYieldOutput,
cCropValueOutput,
cMarginOutput,
bFertiliserAmountOutput,
bFertiliserCostOutput,
bNRateOutput,
bPRateOutput,
bKRateOutput,
bSRateOutput,
bExpectedYieldOutput,
bLossOutput,
bActualYieldOutput,
bCropValueOutput,
bMarginOutput,
comparisonOutput,
bestValue
];
},