Skip to content
Advertisement

How to randomly select array item without repeats and not have the program ask a question twice?

I am developing a trivia program, in which a user gets asked a question at random, and has to enter an answer. The program is telling the user whether they got the question right, and at the end alerts them of their score. Here is my code:

JavaScript

I am unable to find a way to not have the program ask a question twice using an array. May someone please help me? Thank you.

Advertisement

Answer

Here’s a hint, ditch the switch. Have an array of objects with all the questions and answers, like:

JavaScript

Ditch the for loop, use a while loop, and reuse the logic. All you need is the random index

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