Comparing arrays with source questions and user answers I'm trying to realize simple testing application. And I ran into a problem. I have two arrays. The first is an array with the initial questions, the second array is the answers of users. And now I need to count the number of correct answers. I wrote the following code. I wonder: how optimal is it and, if it's bad, how to optimize it?
I tried to change the "filter" to "find", but this did not affect the execution time of the code.



const questions = [
 {
  questionId: 1,
  question: 'question?',
  answers: [
   { value: '1', right: true },
   { value: '2', right: false },
   { value: '3', right: false },
   { value: '4', right: false },
  ],
 },
 {
  questionId: 2,
  question: 'question?',
  answers: [
   { value: '1', right: false },
   { value: '2', right: false },
   { value: '3', right: false },
   { value: '4', right: true },
  ],
 },
 {
  questionId: 3,
  question: 'question?',
  answers: [
   { value: '1', right: false },
   { value: '2', right: true },
   { value: '3', right: false },
   { value: '4', right: false },
  ],
 },
 {
  questionId: 4,
  question: 'question?',
  answers: [
   { value: '1', right: true },
   { value: '2', right: false },
   { value: '3', right: false },
   { value: '4', right: false },
  ],
 },
 {
  questionId: 5,
  question: 'question?',
  answers: [
   { value: '1', right: false },
   { value: '2', right: false },
   { value: '3', right: true },
   { value: '4', right: false },
  ],
 },
]

const answersFromUser = [
 {
  questionId: 1,
  value: '1',
 },
 {
  questionId: 2,
  value: '1',
 },
 {
  questionId: 3,
  value: '1',
 },
 {
  questionId: 4,
  value: '1',
 },
 {
  questionId: 5,
  value: '1',
 },
]

const createObj = (questionId, value, right) =&gt; {
 return { questionId, value, right }
}

const checkAnswers = (questionsArr, answersArr) =&gt; {
 return answersArr.map(answer =&gt; {
  const currentQuestion = questionsArr.filter(
   question =&gt; question.questionId === answer.questionId
  )

  const currentAnswer = currentQuestion[0].answers.filter(
   answerInCurrentQuestion =&gt; answerInCurrentQuestion.value === answer.value
  )[0]

  return createObj(
   answer.questionId,
   currentAnswer.value,
   currentAnswer.right
  )
 })
}

const countRightAnswers = answers =&gt; {
 return answers.filter(answer =&gt; answer.right).length
}

const countWrongAnswers = answers =&gt; {
 return answers.filter(answer =&gt; !answer.right).length
}

console.log(checkAnswers(questions, answersFromUser))
console.log("Right Answers " + countRightAnswers(checkAnswers(questions, answersFromUser)))
console.log("Wrong Answers " + countWrongAnswers(checkAnswers(questions, answersFromUser)))
This is not much of a code review answer, but I don't have time for more right now :(

Here's a stripped down version using only arrays for answers, with indexes (zero to four) as the question numbers. Essentially, it seems like much of your object structures aren't needed at all.



const correctAnswers = [1,4,2,1,3]

const userAnswers = [1,1,1,1,1]

const countRightAnswers = answers =&gt; answers.filter((a, i) =&gt; a == correctAnswers[i]).length

const indexOfRightAnswers = answers =&gt; answers.map((a, i) =&gt; a == correctAnswers[i] ? i : -1).filter(i =&gt; i &gt; -1)

console.log(countRightAnswers(userAnswers))
console.log(indexOfRightAnswers(userAnswers))


Try it online!