...
Code Block |
---|
language | js |
---|
firstline | 1 |
---|
linenumbers | true |
---|
|
function evaluate(answers) {
// preprocess user answers - remove embedded white spaces, and convert to lower case, based on the config
answers = _.map(answers, function (a) {
var ans = a;
if (qData.config.eval_ignore_whitespaces)
ans = ans.trim().replace(/\s/g, '');
if (qData.config.eval_ignore_case)
ans = ans.toLowerCase();
return ans;
});
// get user answer indices. (this is the index in the expAnswers array in which a match is found,
// -1 if no match found)
var userAnsIndices = _.map(answers, function (answer) {
return _.findIndex(qData.data.data.expAnswers, function (expAns) {
return _.contains(expAns, answer);
});
});
// Sort the userAnsIndices array elements in case if unordered evaluation
if (qData.config.eval_unordered)
userAnsIndices = _.sortBy(userAnsIndices, function (u) { return u });
// if the userAnsIndices are found in the expAnsIndices, then the question is sovled!
var isSolved = _.some(qData.data.data.expAnsIndices, function (expAns) {
return _.isEqual(expAns, userAnsIndices);
});
var score = isSolved ? userAnsIndices.length : 0;
// compute partial score, if partial scoring is enabled in config
if (!isSolved && qData.config.partial_score) {
var scoreMap = _.map(userAnsIndices, function (u, i) {
return _.some(qData.data.data.expAnsIndices, function (expAns) {
return qData.config.eval_unordered ? _.contains(expAns, u) : (expAns[i] == u);
}) ? 1 : 0;
});
score = _.reduce(scoreMap, function (memo, num) { return memo + num; }, 0);
}
console.log('Solved', isSolved);
console.log('Score', score / userAnsIndices.length);
}
|
Test code location: https://github.com/ramj-funtoot/groupeval
Iframe |
---|
scrolling | no |
---|
src | https://repl.it/@RamJayaraman/ftb-groupeval?lite=true |
---|
width | 100% |
---|
frameborder | hide |
---|
align | middle |
---|
height | 800px |
---|
|
Run the REPL