我想将用户的不同答案集与正确答案集进行比较,为每个问题计算分数,并为每个问题在该问题的相应索引处分配/计算分数。
我的功能如下:
function evaluateUsersAnswers() {
let point = 10;
let double = 20; //selected double powerup(points)
let NN = 0; //select no negative powerup(points), NN=no negative
let NP = -5; //not selected any powerup(points), NP=no powerup
const correctAnsSet = {"0":"a","1":"b","2":"a","3":"a","4":"b","5":"c","6":"a","7":"b","8":"c","9":"c"};
const user1AnswerSet = {"0":"a","1":"b","2":"b","3":"b","4":"a","5":"c","6":"b","7":"b","8":"c","9":"c"};
const user1PowerUpSet = {"0":[double],"1":[NN],"2":[double],"3":[NN],"4":[NN],"5":[NN],"6":[NP],"7":[NP],"8":[NP],"9":[double]};
const user2AnswerSet = {"0":"b","1":"b","2":"b","3":"b","4":"b","5":"a","6":"b","7":"a","8":"c","9":"b"};
const user2PowerUpSet = {"0":[double],"1":[NN],"2":[double],"3":[NN],"4":[NN],"5":[NN],"6":[NP],"7":[NP],"8":[NP],"9":[double]};
const user1PointsSet = {};
const user2PointsSet = {};
if((Object.values(user1PowerUpSet) === double) || (Object.values(user1PowerUpSet) === NN) || (Object.values(user1PowerUpSet) === NP)) {
}
}
因此,我想将user1AnswerSet
和user2AnswerSet
与correctAnsSet
进行比较,并且我还想根据它们的正确答案计算user1
和user2
的点每个问题并将其存储在user1PointsSet
和user2PointsSet
对象中。
假设第一个问题user1
与correctAnsSet
匹配,而第一个问题他/她使用“双”加电,那么他的分将是20,如果不匹配,则分将是- 20类似地,对于NN
(用户使用的非负加电),如果答案匹配,则分配给该问题的分数将为10,如果不匹配,则分数将为0。类似地,NP
(没有用户使用的电源),如果匹配,则正确答案的得分为10,错误答案的得分为-5。
最后,我想将它们的点(即user1
和user2
)分别存储在user1PointsSet
对象和user2PointsSet
对象中。我可能有多个用户答案集。
我该怎么办?
答案 0 :(得分:1)
您的点的权重/类型应该是字符串或只是其乘数值。
不太确定如何应用点值,但是可以确定答案正确,然后在开关内应用点乘数。
const answerKey = {
questions: [ "a", "b", "a", "a", "b", "c", "a", "b", "c", "c" ],
pointValue : 10
};
var users = [{
id: "user-1",
answers: [
{ choice : "a", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "a", weight : 'double' },
{ choice : "a", weight : 'NN' },
{ choice : "b", weight : 'NN' },
{ choice : "c", weight : 'NN' },
{ choice : "a", weight : 'NP' },
{ choice : "b", weight : 'NP' },
{ choice : "c", weight : 'NP' },
{ choice : "c", weight : 'double' }
]
}, {
id: "user-2",
answers: [
{ choice : "a", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "b", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "a", weight : 'NN' },
{ choice : "c", weight : 'NN' },
{ choice : "b", weight : 'NP' },
{ choice : "b", weight : 'NP' },
{ choice : "c", weight : 'NP' },
{ choice : "c", weight : 'double' }
]
}];
let response = evaluateUsersAnswers(answerKey, users);
console.log(response.map(user => JSON.stringify(user)).join('\n'));
console.log(response.map(user => JSON.stringify({
id : user.id,
total : user.points.reduce((total, curr) => total + curr, 0)
})).join('\n'));
function evaluateUsersAnswers(answerKey, users) {
return users.map(user => {
return {
id : user.id,
points : user.answers.map((answer, index) => {
if (answer.choice === answerKey.questions[index]) {
switch (answer.weight) {
case 'double':
return answerKey.pointValue * 2.0;
case 'NN':
return answerKey.pointValue * 1.0;
case 'NP':
return answerKey.pointValue * 1.5;
}
} else {
switch (answer.weight) {
case 'double':
return answerKey.pointValue * -2.0;
case 'NN':
return 0;
case 'NP':
return answerKey.pointValue * -1.5;
}
}
})
};
});
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
您可以使用权重类型到值的映射来进一步简化此操作。
注意:权重可以与答案键分离。
const answerKey = {
questions: [ "a", "b", "a", "a", "b", "c", "a", "b", "c", "c" ],
pointValue : 10,
weights : {
'double' : { correct: 2.0, incorrect: -2.0 },
'NN' : { correct: 1.0, incorrect: 0.0 },
'NP' : { correct: 1.5, incorrect: -1.5 }
}
};
var users = [{
id: "user-1",
answers: [
{ choice : "a", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "a", weight : 'double' },
{ choice : "a", weight : 'NN' },
{ choice : "b", weight : 'NN' },
{ choice : "c", weight : 'NN' },
{ choice : "a", weight : 'NP' },
{ choice : "b", weight : 'NP' },
{ choice : "c", weight : 'NP' },
{ choice : "c", weight : 'double' }
]
}, {
id: "user-2",
answers: [
{ choice : "a", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "b", weight : 'double' },
{ choice : "b", weight : 'NN' },
{ choice : "a", weight : 'NN' },
{ choice : "c", weight : 'NN' },
{ choice : "b", weight : 'NP' },
{ choice : "b", weight : 'NP' },
{ choice : "c", weight : 'NP' },
{ choice : "c", weight : 'double' }
]
}];
let response = evaluateUsersAnswers(answerKey, users);
console.log(response.map(user => JSON.stringify(user)).join('\n'));
console.log(response.map(user => JSON.stringify({
id : user.id,
total : user.points.reduce((total, curr) => total + curr, 0)
})).join('\n'));
function evaluateUsersAnswers(answerKey, users) {
return users.map(user => {
return {
id : user.id,
points : user.answers.map((answer, index) => {
let correct = answer.choice === answerKey.questions[index];
let weight = answerKey.weights[answer.weight];
let multiplier = weight[correct ? 'correct' : 'incorrect'];
return answerKey.pointValue * multiplier;
})
};
});
}
.as-console-wrapper { top: 0; max-height: 100% !important; }