Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

XML
<!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>


Thanks in advance!


Jerome
Posted

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:
JavaScript
var NoOne = new Array();

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

Instead of this:
JavaScript
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. 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:
JavaScript
//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. 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!
 
Share this answer
 
v4
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:
JavaScript
<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.
 
Share this answer
 
v2
Comments
jump_ace 16-Jun-14 13:24pm    
That seemed to help Matt, but it didn't fully fix it. After about a dozen or so clicks on the Generate! button, number 5 turns to 'undefined' and then one more click and all the FirstFive numbers change to 'undefined'. It's like after the generate button gets pressed the deleted numbers need to be re-added? It's kinda strange.

Jerome
Matt T Heffron 16-Jun-14 14:32pm    
Yes, that's expected!
The FirstFive MUST be reloaded (recreated from scratch) each time you want to generate a SET of numbers.
Each SET is INDEPENDENT!
jump_ace 16-Jun-14 14:36pm    
So I'd have to refresh the webpage every few clicks or so? Is there a way around that so I can click the generate button as many times as I want without seeing the undefined spots?
Matt T Heffron 16-Jun-14 17:57pm    
No, all you should do is to repopulate the FirstFive array in the Generate button click handler.
See the edited solution, above.
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!

JavaScript
<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>
 
Share this answer
 
Comments
Matt T Heffron 16-Jun-14 17:59pm    
See my note above.
Thanks for the quick reply norbitrial! I've used your code, here it is below. Like my method, it runs, however, I still see duplicate numbers. Any suggestions?

JavaScript
<Script Language="JavaScript">
var Test = new Array(1)

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

var NoTwo = new Array();
 for (var i=1; i<60; ++i) {
   NoTwo.push(i);
}

var NoThree = new Array();
 for (var i=1; i<60; ++i) {
   NoThree.push(i);
}

var NoFour = new Array();
 for (var i=1; i<60; ++i) {
   NoFour.push(i);
}

var NoFive = new Array();
 for (var i=1; i<60; ++i) {
   NoFive.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(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(NoPB, false);
}
</SCRIPT>



Jerome
 
Share this answer
 
v2
Comments
norbitrial 13-Jun-14 17:36pm    
I think you can make variable for 60. For instance var arrayLength = 60; and you can use the variable instead of a value in for cycle.
norbitrial 13-Jun-14 17:38pm    
You can set my answer as a solution if it is working. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900