Copy link to clipboard
Copied
Hey gals & guys,
I am making a self-assessment test with 5 questions. Each question has 3 answers. There is no right or wrong answer. Each of the answers is 0, 1 or 2 points. Is there a way to use JavaScript to calculate the final results and output some text in a field and fill it with color? And is there a way to decide the color based on text? Any help would be greatly appreciated!
Use this code as the custom calculation script of the total field:
var total = 0;
for (var i=1; i<=5; i++) {
var v = this.getField("GR"+i).value;
if (v!="Off") total+=Number(v);
}
if (total==0) {
event.value = "";
event.target.fillColor = color.transparent;
} else if (total<5) {
event.value = "Please answer all questions";
event.target.fillColor = color.transparent;
} else if (total>=5 && total<=9) {
event.value = total + " (Good");
event.target.fillColor = color.red;
} else if ((total>=10 && total<=11) || (total>=14 && total<=15)) {
event.value = total + " (Better)");
event.target.fillColor = color.yellow;
} else if (total>=12 && total<=13) {
event.value = total + " (Great)");
event.target.fillColor = color.green;
}
Copy link to clipboard
Copied
Hey gals & guys,
I am making a self-assessment test with 5 questions. Each question has 3 answers. There is no right or wrong answer. Each of the answers is 0, 1 or 2 points. Is there a way to use JavaScript to calculate the final results and output some text in a field and fill it with color? And is there a way to decide the color based on text? Any help would be greatly appreciated!
Use this code as the custom calculation script of the total field:
var total = 0;
for (var i=1; i<=5; i++) {
var v = this.getField("GR"+i).value;
if (v!="Off") total+=Number(v);
}
if (total==0) {
event.value = "";
event.target.fillColor = color.transparent;
} else if (total<5) {
event.value = "Please answer all questions";
event.target.fillColor = color.transparent;
} else if (total>=5 && total<=9) {
event.value = total + " (Good");
event.target.fillColor = color.red;
} else if ((total>=10 && total<=11) || (total>=14 && total<=15)) {
event.value = total + " (Better)");
event.target.fillColor = color.yellow;
} else if (total>=12 && total<=13) {
event.value = total + " (Great)");
event.target.fillColor = color.green;
}
Copy link to clipboard
Copied
Do you just want to show the sum of the selected buttons? If so, it's certainly possible but your choice of values is not so good, as it will not be possible to differentiate between selecting the "0" option and not making any selection at all...
Copy link to clipboard
Copied
Well, I suppose values of 1, 2 and 3 will work. And yes, showing only the sum will also work. Can we based on that sum color a field and display a label or is that a far-fetched idea?
Copy link to clipboard
Copied
Yes, that works better. And sure, that's possible... What color/label do you want to display?
Copy link to clipboard
Copied
Also, what are the names of the radio-button groups?
Copy link to clipboard
Copied
Well, if the color can be red for a sum of 5 to 9, yellow for 10-11 and 14-15, and green for 12-13 that would be great.
And with label "Good" for red, "Better" for yellow, and "Best" for green.
Also the groups are GR1 to GR5 accordingly.
Copy link to clipboard
Copied
What if it's below 5?
Copy link to clipboard
Copied
Well, the questions are 5 so the sum shouldn't be below 5. Maybe for a sum of 1 to 5 display "Please answer all questions" on the label.
Copy link to clipboard
Copied
Use this code as the custom calculation script of the total field:
var total = 0;
for (var i=1; i<=5; i++) {
var v = this.getField("GR"+i).value;
if (v!="Off") total+=Number(v);
}
if (total==0) {
event.value = "";
event.target.fillColor = color.transparent;
} else if (total<5) {
event.value = "Please answer all questions";
event.target.fillColor = color.transparent;
} else if (total>=5 && total<=9) {
event.value = total + " (Good");
event.target.fillColor = color.red;
} else if ((total>=10 && total<=11) || (total>=14 && total<=15)) {
event.value = total + " (Better)");
event.target.fillColor = color.yellow;
} else if (total>=12 && total<=13) {
event.value = total + " (Great)");
event.target.fillColor = color.green;
}
Copy link to clipboard
Copied
It accepts the code but when the radio buttons are selected, nothing happens. Should I assign values to the radio buttons separately and how should the output field be named?
Copy link to clipboard
Copied
Yes, you have to assign "1", "2" and "3" as the "Radio button choice" values for your fields.
The name of the output field doesn't matter.
Copy link to clipboard
Copied
Thank you so much - that worked perfectly!