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:

function askQuestion() {
    score = 0
    for (let step = 0; step < 5; step++) {
        rando = Math.floor(Math.random() * 5)+1;
        switch(rando) {
            case 1:
                var q1 = prompt("Who won the first ever international soccer World Cup? Please write the first letter in capital, and the following ones in lowercase")
                if (q1 == "George Washington") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 2:
                var q2 = prompt("What is a theorem in math for finding a side in a right triangle, knowing 2 others? Please write the both words in capital")
                if (q2 == "Pythagorean Theorem") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 3:
                var q3 = prompt("Who is the first human ever in space? Please write the full name in capital, and the following ones in lowercase")
                if (q3 == "Yuri Gagarin") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 4:
                var q4 = prompt("Who is the first president of the United States? Please write the full name in capital, and the following ones in lowercase")
                if (q4 == "George Washington") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 5:
                var q5 = prompt("In what country were the Olympics invented? Please write the first letter in capital, and the following ones in lowercase")
                if (q5 == "Greece") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 6:
                var q6 = prompt("What is the capital of France? Please capitalize the first letter")
                if (q6 == "France") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 7:
                var q7 = prompt("What is the most purchased video game of all time? Please capitalize the first letter")
                if (q7 == "Minecraft") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 8:
                var q8 = prompt("What is the most watched television brodcast ever? Please write the full name, capitlizing the abbreviation of the organization it is created by, and then the name too.")
                if (q8 == "UEFA Euro 2020") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 9:
                var q9 = prompt("What is the most popular board game in the world? Please capitalize")
                if (q9 == "Chess") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            case 10:
                var q10 = prompt("What year was the U.S. Declaration of Independence written and ratified?")
                if (q10 == "1776") {
                    alert("Correct!")
                    score = score + 1
                } else {
                    alert("Sorry you are incorrect")
                }
                break;
            default:
                alert("This is impossible")
                break;
        }   
    }
    alert("Thanks for playing! Your score is " + score + " out of 5!")
}
askQuestion()

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:

const data: [
  { question: "Who?", answer: "Him" },
  { question: "What?", answer: "That" },
  { question: "Where?", answer: "There" }
]

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

while ( data.length > 0 ) {
   let randomIndex = Math.floor(Math.random() * data.length) + 1;

   var userAnswer = prompt(data[randomIndex].question)

   if (userAnswer === data[randomIndex].answer) {
       alert("Correct!")
       score = score + 1
   } else {
       alert("Sorry you are incorrect")
   }
   data.splice(randomIndex, 1); // remove that question and answer set
}
   
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement