-1

Hello I have a random quote generator. My question is How do I randomly generate an item in an array without getting the same item twice in a row?

below I will post my project I want to keep it a as simple project as possible thank you for any help.

heres is all of my css coding:

#heading {
  color: blue;
}

#containing-square {
  background-color: blue;
  height: 155px;
  width: 400px;
  text-align: center;
  margin: auto 500px;
  border-style: dashed;
}

#quoteDisplay {
  color: yellow;
  line-height: 40px;
  font-size: 20px;
  font-style: oblique;
  display: initial
}

#quote-button {
  margin: auto 660px;
  margin-bottom: 250px;

}

Heres my all of my html coding:


  <div id="containing-square">
    <div id="quoteDisplay">
      Step Into the Mind of Greatness
    </div>
  </div>
  <button id="quote-button" onclick="quote_machine()"> New Quote</button>.  

heres my Javascript code i'm not sure what to add in and where?:

var seenquote = false
    var quotes = [
      "Men are not the creature of circumstances, circumstances are the creatures of men. </br> -Benjamin Desraili ",
      "The secret to life is learning how to use pain and pleasure,if you do that, you are in control in life, if you dont then life controls you. </br> -Anthony Robbins ",
      "Good judgement is the result of experience and experience the result of bad judgement. </br> -Mark Twain",
      "I'd rather die like a man than live like a coward. </br> -Tupac Shakur",
      "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. </br> -Ralph W. Emmerson",
      "To be great is to be misunderstood. </br> -Ralph W. Emmerson",
      "Imagination is everything. It is the preview of life's coming attractions. </br> -Albert Einstein",
      "If you can't explain it simply, you don't understand it well enough. </br> -Albert Einstein",
      "Dude Suckin At Somthing Is The First Step At Being Sorta Good At Somthing. </br> -Jake (Adventure Time)",
      "The Way To Get Started Is To Quit Talking And Begin Doing.<br/> -Walt Disney",
      "Our Greatest Weakness Lies In Giving Up. The Most Certain Way To Succeed Is Always To Try Just One More Time. <br> - Thomas A. Edison",
      "Success Is Somthing you attract not somthing you persue, so instead of going after it you work on yourself. <br> -Jim rohn",
      "Income does not far exceed personal development <br> -",
      "If Someone Hands You A Million Dollars Best You Become A Millionaire Quickly So You Get To Keep The Money",
    ]

function quote_machine() {
      //makes random number 1-14 and stores it in randomnumber
      var randomNumber = Math.floor(Math.random() * (quotes.length));
      //targets quotedisplay and writes the array element located at that random number
      document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
    }
  </script>
</Body>

thank you for any help 
  • add ur code that u have done.. just can solve ur homework here. – xdeepakv May 01 '20 at 05:07
  • Does this answer your question? [Create an array with random values](https://stackoverflow.com/questions/5836833/create-an-array-with-random-values) – xdeepakv May 01 '20 at 05:08

1 Answers1

2

Here is an idea:

  1. Deep copy quotes
  2. Pick a random entry from the deep copy and display it
  3. Remove this entry from the deep copy (to prevent picking the same before picking all the others)
  4. If the deep copy is empty, recopy it

let quotes = [...];
let toPickFrom = [];

function quote_machine() {
      //makes random number 1-14 and stores it in randomnumber
      if(toPickFrom.length == 0){
           toPickFrom = JSON.parse(JSON.stringify(quotes)); //make deep copy
      } 
      var randomNumber = Math.floor(Math.random() * (toPickFrom.length));
      //targets quotedisplay and writes the array element located at that random number
      document.getElementById('quoteDisplay').innerHTML = toPickFrom[randomNumber];
      toPickFrom.splice(randomNumber, 1); //remove from array
    }
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • I'm trying to implement this in but the "let quotes = [...];" gets an error on the 2nd bracket ] – Patrowheels May 01 '20 at 05:41
  • Sorry I've been coding for 2 years and feel like I have for two days .. – Patrowheels May 01 '20 at 05:42
  • 1
    @Patrowheels I did not type your quotes in to save myself time, my intention there was to copy your quotes as you did. – A.J. Uppal May 01 '20 at 05:44
  • Incredible! Thank you @A.J. Uppal! If i may be of some small help to you I seen your A+website and I was very impressed. There may have been a small typo under the graphic design wordage saying "treams" instead of "dreams". Only say that too help if I can but Im very impressed with how much you have learned so young! Thank you ! – Patrowheels May 01 '20 at 05:54
  • Of course :) And thanks for checking it out! Currently revamping it and will be sure to take that into account. Best of luck. – A.J. Uppal May 01 '20 at 05:58