Hello Coders,
Please forgive a very basic Q from someone learning Javascript on the fly. My problem relates to a very complex form in which I need to dyanmically re-calculate some field values if the user changes the values of other fields, all of which happen to be selects. I've created the function, associated it to the onchange for each field that should trigger it, retrieved the necessary field values etc, all fine. My problem is that the arithmetic formula is not working correctly. It's meant to average 6 numeric input values, but I am getting incorrect results. Here's the Javascript function which fetches the field values and re-calculates the dependent values which we call QI's:
function qirecalc() {
const charactervals = { 'Bland': 2, 'Nervous': 2, 'Monotonous': 3, 'Elusive': 3, 'Rustic': 4, 'Determined': 4,
'Lively': 4, 'Invigorating': 4, 'Elegant': 5, 'Ethereal': 5, 'Sublime': 5 };
const richnessvals = {'Harmonic' : 5, 'Balanced' : 4, 'Somewhat Balanced' : 3, 'Limited Balance' : 1};
const complexityvals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.5, 'Poor' : 1 };
const evolutionvals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.8, 'Poor' : 1 };
const valuevals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.5, 'Low' : 1 };
const qualityvals = {'Excellent' : 5, 'Fine' : 4.5, 'Common' : 3.5, 'Poor' : 1};
const persistencevals = {'Very Long' : 5, 'Long' : 4, 'Medium' : 3.5, 'Short' : 1 };
const sensationvals = {'Irritable' : 1, 'Unripe' : 1.25, 'Airy' : 3.5, 'Thick' : 4, 'Well-rounded' : 4.5, 'Smooth' : 4.8, 'Very smooth' : 5 };
var richnessval = richnessvals[document.getElementById("organoleptic_richness").value];
var complexityval = complexityvals[document.getElementById("complexity").value];
var persistenceval = persistencevals[document.getElementById("persistence").value];
var qualityval = qualityvals[document.getElementById("organoleptic_quality").value];
var characterval = charactervals[document.getElementById("character").value];
var sensationval = sensationvals[document.getElementById("key_sensation").value];
var valueval = valuevals[document.getElementById("value_for_price").value];
var evolutionval = evolutionvals[document.getElementById("evolution").value];
var off_qi = document.getElementById("off-flavors_qi").value;
var mqi = Number(document.getElementById("mechanical_qi").value);
var oqi = (richnessval + complexityval + evolutionval + persistenceval + qualityval + off_qi) / 6;
console.log("richnessval = " + richnessval);
console.log("complexityval = " + complexityval);
console.log("persistenceval = " + persistenceval);
console.log("qualityval = " + qualityval);
console.log("off_qi = " + off_qi);
console.log("evolutionval = " + evolutionval);
console.log("oqi = " + oqi);
var cqi = (sensationval + characterval + valueval)/3;
var number = (cqi + oqi + mqi) / 3;
var overall = Math.round(number * 10 ) / 10;
console.log("mqi = " + mqi);
console.log("cqi = " + cqi);
console.log("overall = " + overall);
var oqistr = Math.round(oqi * 10 ) / 10;
var cqistr = Math.round(cqi * 10 ) / 10;
return false;
}
Here's a sample of the console output:
richnessval = 3 ?cigarid=2138:806
complexityval = 3.5 ?cigarid=2138:807
persistenceval = 3.5 ?cigarid=2138:808
qualityval = 4.5 ?cigarid=2138:809
off_qi = 4 ?cigarid=2138:810
evolutionval = 3.8 ?cigarid=2138:811
oqi = 3.0566666666666666
With these values the correct result for oqi is 3.7166666, but I am getting 3.05666. Such a simple formula, I guess I'm over-looking something...
Any suggestions as to why the formula is not written/working correctly would be greatly appreciated!
What I have tried:
function qirecalc() {
const charactervals = { 'Bland': 2, 'Nervous': 2, 'Monotonous': 3, 'Elusive': 3, 'Rustic': 4, 'Determined': 4,
'Lively': 4, 'Invigorating': 4, 'Elegant': 5, 'Ethereal': 5, 'Sublime': 5 };
const richnessvals = {'Harmonic' : 5, 'Balanced' : 4, 'Somewhat Balanced' : 3, 'Limited Balance' : 1};
const complexityvals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.5, 'Poor' : 1 };
const evolutionvals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.8, 'Poor' : 1 };
const valuevals = {'Excellent' : 5, 'Good' : 4, 'Medium' : 3.5, 'Low' : 1 };
const qualityvals = {'Excellent' : 5, 'Fine' : 4.5, 'Common' : 3.5, 'Poor' : 1};
const persistencevals = {'Very Long' : 5, 'Long' : 4, 'Medium' : 3.5, 'Short' : 1 };
const sensationvals = {'Irritable' : 1, 'Unripe' : 1.25, 'Airy' : 3.5, 'Thick' : 4, 'Well-rounded' : 4.5, 'Smooth' : 4.8, 'Very smooth' : 5 };
var richnessval = richnessvals[document.getElementById("organoleptic_richness").value];
var complexityval = complexityvals[document.getElementById("complexity").value];
var persistenceval = persistencevals[document.getElementById("persistence").value];
var qualityval = qualityvals[document.getElementById("organoleptic_quality").value];
var characterval = charactervals[document.getElementById("character").value];
var sensationval = sensationvals[document.getElementById("key_sensation").value];
var valueval = valuevals[document.getElementById("value_for_price").value];
var evolutionval = evolutionvals[document.getElementById("evolution").value];
var off_qi = document.getElementById("off-flavors_qi").value;
var mqi = Number(document.getElementById("mechanical_qi").value);
var oqi = (richnessval + complexityval + evolutionval + persistenceval + qualityval + off_qi) / 6;
console.log("richnessval = " + richnessval);
console.log("complexityval = " + complexityval);
console.log("persistenceval = " + persistenceval);
console.log("qualityval = " + qualityval);
console.log("off_qi = " + off_qi);
console.log("evolutionval = " + evolutionval);
console.log("oqi = " + oqi);
var cqi = (sensationval + characterval + valueval)/3;
var number = (cqi + oqi + mqi) / 3;
var overall = Math.round(number * 10 ) / 10;
console.log("mqi = " + mqi);
console.log("cqi = " + cqi);
console.log("overall = " + overall);
var oqistr = Math.round(oqi * 10 ) / 10;
var cqistr = Math.round(cqi * 10 ) / 10;
return false;
}