Portal Home > Knowledgebase > Articles Database > Javascript Adding weighted numbers
Javascript Adding weighted numbers
Posted by mjfroggy, 11-18-2011, 01:35 PM |
I have a form that contains 4 questions and each question has 3 potential answers with each of the three potential answers being worth a score of 100 or 85 or 65. So right now when someone clicks a radio dial for each of the questions I am using javascript to autosum the radio dials that where checked. However what I need help with is I need each of the 4 questions to be weighted. So question 1 and 2 would be 35% of the total score, question three and four would be 15% of the total score.
My issue is I do not know how to modify the javascript that sums up the scores to weight the individual scores depending on the question. I tried to find some code that would do such a thing and can not find any Suggestions?
to get an idea of how the form is you can check out
http://174.120.2.42/demo/
here is my html and javascript code
|
Posted by mike86, 11-18-2011, 01:55 PM |
I think I understand what you are asking. Do want the total QoSD score to be out of 100?
If so, then multiply each value by a decimal to produce the percentage. So 35% would become 0.35
So for the first question if you got 100, it would be 100*0.35 = 35
Which in javascript would be:
0.35 * parseFloat(100)
Then add up all these values and it gives you a value out of 100.
*edit to correct javascript
Last edited by mike86; 11-18-2011 at 02:08 PM.
|
Posted by mjfroggy, 11-18-2011, 02:18 PM |
Hello
yes so you are correct in understanding what I want to do.
So for question one if the person is given a 100% I would do 100 x .35 which would = 35 but how do I edit the below javascript function which right now just sums the value of the radio dials that have the id of rating . I need it before it sums up the total to first take the selected score and weight it and I am not sure how to edit the javascript code to do that
|
Posted by mike86, 11-18-2011, 02:29 PM |
try this:
this is a pretty rudimentary way to do it, but it should work.
Last edited by mike86; 11-18-2011 at 02:32 PM.
|
Add to Favourites Print this Article
Also Read