banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

JS operates a simple exam grading system for Radiobuttons.

Yesterday and the day before, CG and I took a simple break and adjustment, feeling great. Today, I'm reporting to everyone in the afternoon to check emails. It's about a JS problem, and the problem is roughly as follows:

A cognitive style test with N questions, each with 4 options. Option A corresponds to 0 points, B corresponds to 1 point, and so on. After clicking on the grading button, it can display which style the user belongs to based on the score. For example, 0-10 points belong to Style A, 10-20 points belong to Style B.

Solution: Group four Radiobuttons together, then iterate through each group of Radiobuttons, get the selected value, and add points based on the value. If none of the options in a group are selected, display an error message.

JS code is as follows:

var score; // define score
var rate = new Array(); // define score array
rate['A']=0;
rate['B']=1;
rate['C']=2;
rate['D']=3;
// define question array, each group of questions is an array element
var question = new Array("",
"test1",
"test2",
"test3",
"test4"
);
// add points
function add(grade){
score += rate[grade];
}
// output
function output(){
if(score>=0 && score<=4)
alert("First style");
else if(score >4 && score <=8)
alert("second style");
else if(score >8 && score <=12)
alert("third style");
else
alert("error");
}
// get value of radio button
function getRadioValue(radioName){
var obj=document.getElementsByName(radioName);
for(var i=0;i<obj.length;i++){
if(obj[i].checked){
return obj[i].value;
}
}
}

Demo link:
http://www.lidaren.com/code/judge.htm

Tomorrow, work will start again. I wish everyone a happy Labor Day holiday.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.