Array Comparison

Array Comparison

How can I compare two arrays and extract the indexes for all the differences? Using:

  1. var diff = $(correctAnswer).not(userAnswer).get();


will only give me the values that appear in 'correctAnswer', which don't appear in 'userAnswer' (i.e. 2 and 8). What I want to be able to do is to compare the arrays in order and then extract the index numbers for any differences, so e.g., in this sample it would be:

index 1 (the correct answer is 2, the user answer is 3)

index 2 (the correct answer is 3, the user answer is 4)

index 7 (the correct answer is 8, the user answer is 7)


  1. var correctAnswer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
  2. var userAnswer = [1, 3, 4, 4, 5, 6, 7, 7, 9, 10, 11, 12];

  3. var diff = $(correctAnswer).not(userAnswer).get()
  4. console.log(diff);