0

I am starting in Javascript and about as beginner as you get, I found this random quote generator but trying to figure out how to make it so it doesn't repeat any of them as I would eventually like to add a massive list of quotes and it just goes through them with no repeating.

var Quotation=new Array() 

Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
sirandy
  • 1,834
  • 5
  • 27
  • 32

2 Answers2

0

One way would be to remove quotes as they are chosen from the available pool.

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
Quotation.splice(whichQuotation, 1); //<-- this is how we remove it from the array

splice() allows us to change the contents of the array, including, as in our case, deleting elements.

The first argument passed denotes the index (of the array elements) at which we want to start deletion. The second argument denotes how many elements to delete from that point.

Another way would have been:

delete Quotation[whichQuotation];
Quotation = Array.filter(val => val);

The first line deletes the array element, but leaves its empty slot. We then gather up the remaining non-empty elements of the array by running a simple filter on it, which says that only those slots which are not empty should remain.

Mitya
  • 33,629
  • 9
  • 60
  • 107
0

You could store the indices of the quotes already used persistently (so that they survive page reloads), the generate a new index until one is found that isn't repeating:

  // helper to store arrays persistently
  function getArray(name) {
    return JSON.parse(localStorage.getItem(name)) || [];
  }

  function storeArray(name, indices) {
    localStorage.setItem(name, JSON.stringify(indices));
  }


  function getNonRepeating(name, array) {
     let used = getArray(name);
     // All quotes were shown already, restart
     if(used.length == array.length)
       used = [];

     // Generate a new index as long as it is already used
     let index;
     do {
       index = Math.floor( Math.random() * array.length );
     } while(used.includes(index))

     // Add the index to the used ones:
     used.push(index);
     // Store them:
     storeArray(name, used);

     return array[index];
 }

 // Array literals make the code much more readable:
 const quotes = [
   "a",
   "b",
   "c"
 ];

 // document.write() is deprecated, instead manipulate the DOM:
 document.body.textContent = getNonRepeating("quote", quotes);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151