How do I weight certain array comparisons more heavily than others when matching input across an answer key?

2 weeks ago 16
ARTICLE AD BOX

I'm making a quiz-type program and I have 8 preset answer keys that each have an array of 15 binary values. User answers the same 15 questions, I compare their array against all 8 and then return the closest match. My main concern is that some of the presets only differ on like 2-3 values so the "winner" is essentially arbitrary. The 2 questions where they disagree should matter more than the 13 where they don't.

Is there a way to detect which values actually differentiate between presets and weight those higher automatically, or is there some sort of algorithm for this? I could hardcode weights for every question I guess but that feels inherently wrong due to me somehow gaming what should be somewhat objective results, ideally the weights would be computed through the presets themselves.

For context I've made sure to add a code snippet, the presets represent historical philosophers and the questions themselves are specific trolley problems. I've made sure to convert the bools to 0s and 1s to facilitate any possible algorithmic intervention that I could get. Thanks.

const philosophers = [ { name: "Nietzsche", answers: [1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1] }, { name: "Diogenes", answers: [1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] }, { name: "Aquinas", answers: [0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0] } ]; function findMatch(userAnswers) { let bestMatch = ""; let highScore = 0; for (let i = 0; i < philosophers.length; i++) { let score = 0; for (let j = 0; j < userAnswers.length; j++) { if (userAnswers[j] === philosophers[i].answers[j]) { score++; } } if (score > highScore) { highScore = score; bestMatch = philosophers[i].name; } } return bestMatch; }
Read Entire Article