Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

// Declare function to re-calculate QI's whenever a key field value changes
function qirecalc() {
	// Setup LOV arrays	
	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 };
    
	// Get the field values used to calculate the Organoleptic, Conclusive and Overall Quality Indexes
	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);

	// Re-calculate the Organoleptic, Conclusive and Overall Quality Indexes
	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);
    
	// Update the Organoleptic, Conclusive & Overall QI fields with their new values and exit
	var oqistr = Math.round(oqi * 10 ) / 10;
	var cqistr = Math.round(cqi * 10 ) / 10;
	// document.forms['consol-form']['organoleptic_qi'].value = oqistr;
	// document.forms['consol-form']['conclusive_considerations_qi'].value = cqistr;
	// document.forms['consol-form']['overall_quality_index'].value = overall;
    
	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:

// Declare function to re-calculate QI's whenever a key field value changes
function qirecalc() {
	// Setup LOV arrays	
	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 };
    
	// Get the field values used to calculate the Organoleptic, Conclusive and Overall Quality Indexes
	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);

	// Re-calculate the Organoleptic, Conclusive and Overall Quality Indexes
	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);
    
	// Update the Organoleptic, Conclusive & Overall QI fields with their new values and exit
	var oqistr = Math.round(oqi * 10 ) / 10;
	var cqistr = Math.round(cqi * 10 ) / 10;
	// document.forms['consol-form']['organoleptic_qi'].value = oqistr;
	// document.forms['consol-form']['conclusive_considerations_qi'].value = cqistr;
	// document.forms['consol-form']['overall_quality_index'].value = overall;
    
	return false;
}
Posted
Updated 14-Nov-22 6:45am
v2
Comments
Chris Copeland 14-Nov-22 11:47am    
I'm unable to reproduce this result. I copy + pasted the exact code above into JS fiddle, then manually set each of the variables to the values you've described and I get the correct answer. There must be more to this than what we can see. For example, could some of those variables actually be a string instead of a number?, that could affect the result.

I will say this, if you sum together all the values except the "4" from the off_qi variable you end up with 3.05, which is close.
David Wells 2022 14-Nov-22 12:29pm    
Thanks Chris for taking a shot at this - you're right there's more to it. let me ad the entire function, since the error may be elsewhere...
Member 15627495 14-Nov-22 12:41pm    
It's a main Js advice : for maths.
var oqi = eval( (richnessval + complexityval + evolutionval + persistenceval + qualityval + off_qi) / 6 );
David Wells 2022 14-Nov-22 12:42pm    
Thanks again Chris - your comment led me to the answer. Indeed the off_qi was a string as I fethced that one from a text input. Man I lost alot of hours on that simple thing, but again I appreciate your time and help to resolve it!

1 solution

As Chris pointed out, one of my input variables was a string, as it was being fetched from a text input. The offender was the off_qi variable and when I changed the fetch to convert it to a number (as I had already done with the mqi variable) suddenly problem was resolved:

var off_qi = Number(document.getElementById("off-flavors_qi").value);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900