Skip to content
Advertisement

How to generate four choices for users without two choices being the same?

I’m using an API to create an anime quiz. I’m also using Math.random() to create four choices for the user to click on. But I’m facing two problems. Firstly when the user is presented with the first set of 4 choices, there’s a possibility that two are identical. I’d like all four choices to be distinct from each other. Secondly regardless of the user getting the right answer or not I’d like another set of four distinct questions to be generated. I tried to come up with something but it quickly turned into spaghetti code.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

I little refactored your code and fixed bugs. And now you can get 4 random answers but with guarantee that there is correct answer. Also now the number of answers depends on count of div with class answer:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement