0

I am trying to make a Thanksgiving Bingo generator and want to make it so the phrases appear only once.

Not sure what direction to take. Here is the code so far:

var questions = [
    "Can name all 3 Pilgrim ships",
    "Plays football",
    "Has an unusual Thanksgiving tradition",
    "Has a turkey disaster story",
    "Vegetarian",
    "Loves cranberry sauce",
    "Has celebrated Thanksgiving in another country",
    "Can name 5 things grateful for",
    "Makes a mean green bean casserole",
    "Eats mac and cheese on Thanksgiving",
    "Has worked retail on Black Friday",
    "Thanksgiving is favorite holiday",
    "Has seen a turkey in real life",
    "Watched the Macy's T-day parade in person",
    "Willing to share pie recipe",
    "Has attended a Friendsgiving",
    "Loves leftovers",
    "Dines out for Thanksgiving",
    "Can name 5 native American tribes",
    "Watches football",
    "Can gobble like a turkey",
    "Celebrates Canadian Thanksgiving",
    "Hates cranberry sauce",
    "Goes Black Friday shopping"
]

function newQuestion() {
 
    var randomNumber = Math.floor(Math.random() * (questions.length));
        document.getElementById('question-display').innerHTML = questions[randomNumber];
       
    }
  • Does this answer your question? [Seeding the random number generator in Javascript](https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript) – zero298 Nov 13 '20 at 20:50
  • 2
    If I correctly understand what you want, the simplest way would be to [sort the array randomly](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array), and then display the results one at a time in order. – Daniel Beck Nov 13 '20 at 20:51
  • 4
    Shuffle and pop – epascarello Nov 13 '20 at 20:57
  • https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – epascarello Nov 13 '20 at 21:49

1 Answers1

0

I think Shuffle and pop like @epascarello commented out is the perfect way in this case, here is an example:

var questions = [
    "Can name all 3 Pilgrim ships",
    "Plays football",
    "Has an unusual Thanksgiving tradition",
    "Has a turkey disaster story",
    "Vegetarian",
    "Loves cranberry sauce",
    "Has celebrated Thanksgiving in another country",
    "Can name 5 things grateful for",
    "Makes a mean green bean casserole",
    "Eats mac and cheese on Thanksgiving",
    "Has worked retail on Black Friday",
    "Thanksgiving is favorite holiday",
    "Has seen a turkey in real life",
    "Watched the Macy's T-day parade in person",
    "Willing to share pie recipe",
    "Has attended a Friendsgiving",
    "Loves leftovers",
    "Dines out for Thanksgiving",
    "Can name 5 native American tribes",
    "Watches football",
    "Can gobble like a turkey",
    "Celebrates Canadian Thanksgiving",
    "Hates cranberry sauce",
    "Goes Black Friday shopping"
].sort(_=> Math.random() - 0.5);

document.querySelector("#get-question").onclick = function() {
  document.getElementById('question-display').innerHTML = questions.pop() || "There are no more questions!";    
}
<p  id="question-display"></p>
<button id="get-question">Get new question</button>
Saadi Toumi Fouad
  • 2,779
  • 2
  • 5
  • 18