如何在Javascript中生成非重复数字 - 强力球 [英] How to generate non-repeating numbers in Javascript - Powerball

查看:144
本文介绍了如何在Javascript中生成非重复数字 - 强力球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Powerball随机发生器,我差不多完成了。我需要帮助才能让前5个数字不再重复(第6个,PowerBall号码可以重复)。我不知道如何处理它,因为我仍然是编写代码的新手。欢迎提供有关如何操作和/或如何改进现有代码的任何建议和提示。



I'm trying to build a Powerball randomizer and I'm nearly finished. I need help with getting the first 5 numbers to no repeat themselves (the 6th, PowerBall number can repeat). I'm not sure how to tackle it as I'm still pretty new to writing code. Any suggestions and tips on how to do it and/or how to improve my current code is welcome.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jump_Ace's PowerBall Generator</title>
    </head>
<body>
<center>
<Script Language="JavaScript">
var Test = new Array(1)

var NoOne = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)

var NoTwo = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)

var NoThree = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)

var NoFour = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)

var NoFive = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)

var PB = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35)

var a=0 // Test
var b=0 // NoOne
var c=0 // NoTwo
var d=0 // NoThree
var e=0 // NoFour
var f=0 // NoFive
var g=0 // PB

function PowerBall()
{
a=Math.floor(Math.random() * Test.length);

if(Test[a]=1)
{
b=Math.floor(Math.random() * NoOne.length);
c=Math.floor(Math.random() * NoTwo.length);
d=Math.floor(Math.random() * NoThree.length);
e=Math.floor(Math.random() * NoFour.length);
f=Math.floor(Math.random() * NoFive.length);
g=Math.floor(Math.random() * PB.length);
document.FORM.NoA.value=NoOne[b]
document.FORM.NoB.value=NoTwo[c]
document.FORM.NoC.value=NoThree[d]
document.FORM.NoD.value=NoFour[e]
document.FORM.NoE.value=NoFive[f]
document.FORM.NoPB.value=PB[g]
}

}
</SCRIPT>
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#E4E4E4" bordercolor="#FFFFFF">
<tr>
<td height="180" bgcolor="#FFFFFF" TD ALIGN="CENTER"><font face="Palatino Linotype" size="5" color="#2A759D">Jump_Ace's PowerBall Generator</font>
<br />
<br />
<CENTER>
<FORM NAME="FORM">
<TABLE>
<TR>
<TD VALIGN="TOP" TYPE="TEXT" NAME="Set1" SIZE="75" style="text-align:center; width: 50px;"><font face="Palatino Linotype" size="3" color="#2A759D">Set 1:</font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoA" SIZE="75" style="text-align:center; width: 30px;"></font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoB" SIZE="75" style="text-align:center; width: 30px;"></font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoC" SIZE="75" style="text-align:center; width: 30px;"></font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoD" SIZE="75" style="text-align:center; width: 30px;"></font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoE" SIZE="75" style="text-align:center; width: 30px;"></font></TD>
<TD VALIGN="TOP"><font face="Arial" size="2"><INPUT TYPE="TEXT" NAME="NoPB" SIZE="75" style="text-align:center; width: 30px; color: #FF0000;"></font></TD>
</TR>
</TABLE>
<P ALIGN="CENTER"><input type="BUTTON" value="Generate!" onclick="PowerBall()" /></P>
</FORM>
</CENTER>
</td>
</tr>
</table>
<br />
</center>
</body>
</html>





提前致谢!





Jerome



Thanks in advance!


Jerome

推荐答案

我有一些建议你,我希望我可以帮你这个!





1.首先我建议你的代码行后面总是使用分号。



2.你不需要将一个一个值推入一个数组,你可以使用for或while循环,如下所示:

I have some suggestions for you, I hope I can help you with this!


1. First of all I suggest always use semicolon after your line of code.

2. You don't need to push one by one values into an array, you can do that with for or while cycle as follow:
var NoOne = new Array();

for (var i=1; i<60; ++i) {
   NoOne.push(i);
}



而不是:


Instead of this:

var NoOne = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59)





3.最后最好为重复代码块创建函数。它总是可以帮助您更好地阅读代码,如果您需要修改代码中的某些内容,则可以更轻松地在一个地方更改所有内容。最后这是我的解决方案:



3. And for the last it's better to make functions for repeating code blocks. It always helps you to read better the code and if you need to modify something in your code it's easier to change everything in one place. And finally here is my solution:

//this function will delete elements from your array
var RemoveElement = function(myArray, randomItem) {
   var index = myArray.indexOf(randomItem);

   //this will delete the actual array element
   if (index > -1) {
      myArray.splice(index, 1);
   }
}

//this function gets a random value and you can set a deleteFlag in order to decide remove or not that from the given array
var GetRandomElement = function(myArray, deleteFlag) {
   var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

   if (deleteFlag == true) {
      RemoveElement(myArray, randomItem);
   }

   return randomItem;
}

//and you simple need to call PowerBall() function in order to play your game
var PowerBall = function() {
   document.FORM.NoA.value = GetRandomElement(NoOne, true);
   document.FORM.NoB.value = GetRandomElement(NoTwo, true);
   document.FORM.NoC.value = GetRandomElement(NoThree, true);
   document.FORM.NoD.value = GetRandomElement(NoFour, true);
   document.FORM.NoE.value = GetRandomElement(NoFive, true);
   document.FORM.NoPB.value = GetRandomElement(PB, false);
}





+1。对于最后的提示。从数组中删除所有元素后,您应该考虑数组长度检查或编写函数以重置数组中的值!





我建议阅读关于JavaScript的最佳实践,有很多好文章。我将从这个开始[ ^ ]一个。



我希望我能帮助你一点点写这篇文章!



+1. For the last hint. After you deleted all the elements from array you should consider an array length checking or write a function in order to reset values in your arrays!


I suggest to read best practices about JavaScript there are many good articles. I would start with this[^] one.

I hope I could help you a little with this writing!


你不想为第一个使用不同的数组5个数字。使用相同的数组,以便您可以跟踪已经选择的数字:

You don't want to use different arrays for the first 5 numbers. Use the same array so you can track which numbers have been selected already:
<script language="JavaScript" mode="hold" />
// EDITED: MTH -- moved to below
//var FirstFive = new Array();
// for (var i=1; i<60; ++i) {
//   FirstFive.push(i);
//}
 
var NoPB = new Array();
 for (var i=1; i<36; ++i) {
   NoPB.push(i);
}
 
//this function will delete ONE element from the array
var RemoveElement = function(myArray, item) {
   var index = myArray.indexOf(itemtem);
 
   //this will delete the actual array element
   if (index > -1) {
      myArray.splice(index, 1);
   }
}
 
//this function gets a random value and you can set a deleteFlag in order to decide whether to remove that from the given array
var GetRandomElement = function(myArray, deleteFlag) {
   var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
 
   if (deleteFlag == true) {
      RemoveElement(myArray, randomItem);
   }
 
   return randomItem;
}
 
//and you simply need to call PowerBall() function in order to play your game
var PowerBall = function() {
// EDIT: MTH -- moved here
   var FirstFive = new Array();
    for (var i=1; i<60; ++i) {
      FirstFive.push(i);
   }
   document.FORM.NoA.value = GetRandomElement(FirstFive, true);
   document.FORM.NoB.value = GetRandomElement(FirstFive, true);
   document.FORM.NoC.value = GetRandomElement(FirstFive, true);
   document.FORM.NoD.value = GetRandomElement(FirstFive, true);
   document.FORM.NoE.value = GetRandomElement(FirstFive, false);
   document.FORM.NoPB.value = GetRandomElement(NoPB, false);
}
</SCRIPT>





Edit: Matt T Heffron - moved the FirstFive array to local scope.

The NoPB could be moved to local scope as well, but since it never is changed, there’s really no need to do so.



Matt T Heffron - moved the FirstFive array to local scope.
The NoPB could be moved to local scope as well, but since it never is changed, there's really no need to do so.


I found a workaround all on my own *gasp!* :)



I just added a reload command at the end of the function so it reloads the page every time you click the Generate button. Here’s my final code for one set of numbers. Thanks for your help guys!



I found a workaround all on my own *gasp!* :)

I just added a reload command at the end of the function so it reloads the page every time you click the Generate button. Here's my final code for one set of numbers. Thanks for your help guys!

<Script Language="JavaScript">
var FirstFive = new Array();
 for (var i=1; i<60; ++i) {
   FirstFive.push(i);
}
 
var NoPB = new Array();
 for (var i=1; i<36; ++i) {
   NoPB.push(i);
}

//this function will delete elements from your array
var RemoveElement = function(myArray, randomItem) {
   var index = myArray.indexOf(randomItem);
 
   //this will delete the actual array element
   if (index > -1) {
      myArray.splice(index, 1);
   }
}
 
//this function gets a random value and you can set a deleteFlag in order to decide whether to remove that from the given array
var GetRandomElement = function(myArray, deleteFlag) {
   var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
 
   if (deleteFlag == true) {
      RemoveElement(myArray, randomItem);
   }
 
   return randomItem;
}
 
//and you simply need to call PowerBall() function in order to play your game
var PowerBall = function() {
   document.FORM.NoA.value = GetRandomElement(FirstFive, true);
   document.FORM.NoB.value = GetRandomElement(FirstFive, true);
   document.FORM.NoC.value = GetRandomElement(FirstFive, true);
   document.FORM.NoD.value = GetRandomElement(FirstFive, true);
   document.FORM.NoE.value = GetRandomElement(FirstFive, true);
   document.FORM.NoPB.value = GetRandomElement(NoPB, false);
   location.reload();
}
</SCRIPT>


这篇关于如何在Javascript中生成非重复数字 - 强力球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆