Friday, August 14, 2015
function Examination(placeHolderId) {
var _q = [], _qi = [], ph = placeHolderId;
var self = this;
function _add(qn, q, op, im, ai) {
_qi[_qi.length] = qn;
_q[qn] = {};
_q[qn]['Q'] = q;
_q[qn]['O'] = op;
_q[qn]['M'] = im;
_q[qn]['A'] = ai;
}
function _renderQue() {
var i, j, rc, row, col, c, tbl, tb, type, btn, opt;
c = document.getElementById(ph);
tbl = document.createElement('table');
tbl.id = ph + '_tblQuestions';
tbl.border = '1';
tbl.width = '100%';
tbl.cellPadding = '5';
tbl.cellSpacing = '0';
c.appendChild(tbl);
tb = document.createElement('thead');
tbl.appendChild(tb);
row = tb.insertRow(0);
col = row.insertCell(0);
col.align = 'center';
col.style.fontWeight = 'bold';
col.innerHTML = 'Question No#';
col = row.insertCell(1);
col.align = 'center';
col.style.fontWeight = 'bold';
col.innerHTML = 'Question';
col = row.insertCell(2);
col.align = 'center';
col.style.fontWeight = 'bold';
col.innerHTML = 'Options';
tb = document.createElement('tbody');
tbl.appendChild(tb);
for(i = 0; i < _qi.length; i++) {
rc = tb.rows.length;
row = tb.insertRow(rc);
col = row.insertCell(0);
col.width = '15%';
col.style.verticalAlign = 'top';
col.innerHTML = _qi[i];
col = row.insertCell(1);
col.width = '45%'
col.style.verticalAlign = 'top';
col.innerHTML = _q[_qi[i]].Q;
col = row.insertCell(2);
col.width = '45%';
col.style.verticalAlign = 'top';
type = _q[_qi[i]].M ? 'checkbox' : 'radio';
for(var j = 0; j < _q[_qi[i]].O.length; j++) {
col.innerHTML += ' ' + _q[_qi[i]].O[j] + '
';
}
}
tb = document.createElement('tfoot');
tbl.appendChild(tb);
rc = tb.rows.length;
row = tb.insertRow(rc);
col = row.insertCell(0);
col.colSpan = 4;
col.align = 'center';
btn = document.createElement('input');
btn.type = 'button';
btn.value = 'Submit';
btn.onclick = function() { self.renderResult(); }
col.appendChild(btn);
}
function _renderRe() {
var i, j, rc, row, col, c, tbl, tb, type, opt, mi = '', checked = true, res, cnt = 0;
for(i = 0; i < _qi.length; i++) {
opt = document.getElementsByName('Question'+i);
for(j = 0; j < _q[_qi[i]].O.length; j++) {
checked = opt[j].checked;
if(checked) break;
}
if(!checked) mi += _qi[i] + '\n';
}
if(mi.length > 0) {
alert('Please provide the answers for following questions - \n' + mi);
return false;
}
tbl = document.getElementById(ph + '_tblQuestions');
col = tbl.tHead.rows[0].insertCell(tbl.tHead.rows[0].cells.length);
col.align = 'center';
col.style.fontWeight = 'bold';
col.innerHTML = 'Result';
tb = tbl.tBodies[0];
for(i = 0; i < _qi.length; i++) {
row = tb.rows[i];
col = row.insertCell(row.cells.length);
col.style.verticalAlign = 'top';
opt = document.getElementsByName('Question'+i);
checked = false;
if(_q[_qi[i]].M) {
for(j = 0; j < _q[_qi[i]].O.length; j++) {
if(opt[j].checked) {
for(c = 0; c < _q[_qi[i]].A.length; c++) {
checked = (parseInt(_q[_qi[i]].A[c],10) == parseInt(opt[j].value,10));
if(checked) break;
}
if(!checked) break;
}
}
}
else {
checked = opt[_q[_qi[i]].A[0]].checked;
}
res = checked ? 'Correct' : 'Wrong';
if(checked) cnt++;
col.innerHTML = res;
col = tbl.tFoot.rows[0].cells[0];
col.innerHTML = cnt + ' of ' + _qi.length + ' questions are correct.
Result = ' + Math.round(cnt/_qi.length*100).toFixed(2) + '%';
}
}
this.addQuestion = function(QuestionNumber, Question, OptionsArray, isMultiple, AnswerIndexArray) {
_add(QuestionNumber, Question, OptionsArray, isMultiple, AnswerIndexArray);
}
this.renderQuestions = function() {
_renderQue();
}
this.renderResult = function() {
_renderRe();
}
}
(2) Create HTML file
No related posts
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment