【问题标题】:How do I randomly generate an item in an array without getting the same item twice in a row?如何在数组中随机生成一个项目而不连续两次获得相同的项目?
【发布时间】:2020-08-15 14:55:17
【问题描述】:

您好,我有一个随机报价生成器。我的问题是如何在数组中随机生成一个项目而不连续两次获得相同的项目?

我将在下面发布我的项目,我想让它尽可能简单,谢谢你的帮助。

这是我所有的 css 编码:

#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;

}

这是我所有的 html 编码:


  <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>.  

这是我的 Javascript 代码,我不确定要添加什么以及在哪里添加?:

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 

【问题讨论】:

标签: javascript html css


【解决方案1】:

这是一个想法:

  1. 深拷贝quotes
  2. 从深层副本中选择一个随机条目并显示它
  3. 从深层副本中删除此条目(以防止在选择所有其他条目之前先选择相同的条目)
  4. 如果深拷贝为空,则重新拷贝它

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
    }

【讨论】:

  • 我正在尝试实现这一点,但“让引号 = [...];”在第二个括号中出现错误]
  • 对不起,我已经编码了 2 年,感觉就像我有两天一样..
  • @Patrowheels 我没有输入您的报价以节省自己的时间,我的目的是像您一样复制您的报价。
  • 难以置信!谢谢@A.J。乌帕尔!如果我对你有一点帮助的话,我看过你的 A+ 网站,我印象非常深刻。在平面设计的文字下可能有一个小错字,说“treams”而不是“dreams”。如果可以的话,只能说这太有帮助了,但我对你这么年轻就学到了很多东西印象深刻!谢谢!
  • 当然 :) 感谢您的检查!目前正在对其进行改造,并且一定会考虑到这一点。祝你好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 2015-09-15
  • 1970-01-01
相关资源
最近更新 更多