const QUESTION_TYPE = {
DANXUAN: 1,
DUOXUAN: 2,
PANDUAN: 3,
TIANKONG: 4
}
let answers = [
{
answerId:['唐老鸭','公鸭','不是'],
},
{
answerId:2
},
{
answerId:[3,2]
},
{
answerId:1
}
]
let rightAnswers = [
{
answerId:['唐老鸭','公鸭','母鸭'],
questionType:4,
socre:'3'
},
{
answerId:2,
questionType:1,
score:'2'
},
{
answerId:[3,2],
questionType:2,
socre:'2'
},
{
answerId:1,
questionType:3,
score:'2'
}
]
function isIncludes(arr1, arr2) {
return arr2.every(val => arr1.includes(val));
}
function calculate(answers, rightAnswers) {
let score = 0
rightAnswers.map((item, idx) => {
if ((rightAnswers[idx].questionType == QUESTION_TYPE.DANXUAN || rightAnswers[idx].questionType == QUESTION_TYPE.PANDUAN) && rightAnswers[idx].answerId == answers[idx].answerId ) {
score += Number(rightAnswers[idx].score)
}
else if (rightAnswers[idx].questionType == QUESTION_TYPE.DUOXUAN ) {
if(rightAnswers[idx].answerId.length ==answers[idx].answerId.length && isIncludes(rightAnswers[idx].answerId,answers[idx].answerId) ) {
score += Number(rightAnswers[idx].score)
}
}
// else if (rightAnswers[idx].questionType == QUESTION_TYPE.TIANKONG) {
// let count = 0
// rightAnswers[idx].answerId.map((val1, i) => {
// if (rightAnswers[idx][i] == answers[idx][i]) {
// count++
// }
// })
// if (count > 0) {
// score += (count / rightAnswers[idx].answerId.length) * Number(rightAnswers[idx].score)
// }
// }
})
return score
}
console.log(calculate(answers, rightAnswers))
console