-2

I need to write a function that checks if the value in first array (a number in userAnswers) appears in second array (array of number arrays - correctAnswers) for the same question index. For example, if 1 or 2 is in userAnswers, and correctAnswers has [1, 2] as its value for userAnswers[0] and correctAnswers[0], return true, else return false.

integral100x
  • 332
  • 6
  • 20
  • 47
  • mat-checkbox questions can have more than one answer as described above, so I'm not sure how to handle that. – integral100x Jun 08 '20 at 19:15
  • Does this answer your question? [Check if every element in one array is in a second array](https://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array) – Heretic Monkey Jun 08 '20 at 19:16

2 Answers2

1

var userAnswers=[1,5,3,4,7];
var correctAnswers=[1,3,8,4,6];

function findMatch(userAnswers, correctAnswers) {
  var ary = new Array();
  for(i = 0;i < correctAnswers.length; i++)
  {  
      ary.push(correctAnswers[i] == userAnswers[i] ? true :false);   
  }
  return ary;
}

function CheckByIndex(i){
  return correctAnswers[i] == userAnswers[i] ? true :false
}

console.log(findMatch(userAnswers,correctAnswers))
console.log(CheckByIndex(0))
console.log(CheckByIndex(2))
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
0
const userAnswers = [5,9,1,2,3];
const correctAnswers = [5,9,1,2,5];
const result = [];

userAnswers.forEach((element,index) => {
    result[index] = element === correctAnswers[index]
})

// result 
// [true, true, true, true, false]

const userAnswers = [5,9,1,2,3];
const correctAnswers = [5,9,1,2,5];
const result = userAnswers.every((element,index) => element === correctAnswers[index])

// result 
// false

Ahmed ElMetwally
  • 2,276
  • 3
  • 10
  • 15