I am developing a quiz application with questions. I can not solve the problem or solve it very crookedly. There are two arrays:

User Responses:

0:{questionId: "1", answerId: "1"} 1:{questionId: "2", answerId: "4"} 2:{questionId: "2", answerId: "6"} 3:{questionId: "3", answerId: "8"} 

And the correct answers:

 0:{questionId: "1", answerId: "3"} 1:{questionId: "2", answerId: "4"} 2:{questionId: "2", answerId: "6"} 3:{questionId: "3", answerId: "7"} 

I need to compare two arrays and get something like this:

 0:{questionId: "1", result: false} \\ пользователь дал не правильный ответ 1:{questionId: "2", result: true} \\ пользователь дал правильный ответ по второму вопросу, в нем два варианта ответа оба совпали. Иначе false если один из ответов не верен 2:{questionId: "3", result: false} \\ пользователь дал не правильный ответ 

I can’t do this at all. How to solve this problem?

My decision:

 @section scripts { <script> $(document).ready(function () { let userAnswers = []; let answers = []; $('#checkResultBtn').click(function () { $('input:checked', '.card-body').each(function () { userAnswers.push({ questionId: $(this).attr('name'), answerId: $(this).val() }); }); console.log(userAnswers); console.log(answers); diff(userAnswers, answers); }); function diff(userAnswers, answers) { userAnswers.forEach(function (userAns) { answers.forEach(function (ans) { if (userAns['questionId'] == ans['questionId']) { if (userAns['answerId'] == ans['answerId']) console.log(true) else console.log(false) } }); }); } @foreach(var answers in Model){ var answersList = answers.Answers.Where(x => x.Answer == 1); foreach(var answer in @answersList) { <text> answers.push({ questionId: '@answer.QuestionId', answerId: '@answer.Id' }); </text> } } }); </script> } 

https://pastebin.com/B2K5z9UX

PS
It is worth considering that the array on the part of the user may not be the same length as the array of correct answers.
The user can not answer the question at all.

  • How did you solve it? Or decide. - user207618
  • While it is very bad, we can say that in any way. I compare them in a cycle. - shatoidil
  • Put the code, please. - user207618
  • Can you change the generation structure of these structures? They are wrong now, you need to do something else. - user207618

1 answer 1

 var ans = [ {questionId: "1", answerId: "1"}, {questionId: "2", answerId: "4"}, {questionId: "2", answerId: "6"}, {questionId: "3", answerId: "8"}, ]; var keys = [ {questionId: "1", answerId: "3"}, {questionId: "2", answerId: "4"}, {questionId: "2", answerId: "6"}, {questionId: "3", answerId: "7"}, ]; var keys2 = Object.create(null); for (var x of keys) { (keys2[x.questionId] = keys2[x.questionId] || []).push(x.answerId); } for (var x of Object.keys(keys2)) { keys2[x].sort(); } var ans2 = Object.create(null); var res = []; for (var x of ans) { (ans2[x.questionId] = ans2[x.questionId] || []).push(x.answerId); } for (var x of Object.keys(ans2)) { res.push({questionId: x, result: ans2[x].sort() + '' == keys2[x]}); } console.log(res); 
 .as-console-wrapper.as-console-wrapper { max-height: 100vh }