Function inside a for loop: Generator function? Help on small project.

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Function inside a for loop: Generator function? Help on small project.

Hey everyone, this is my first post. I have finished a JavaScript tutorial on codeacademy.org’s site a few days ago. It took me a few months or so to complete it.

I figure now I have the basic understanding of Object Oriented Programing to dive into a small project I have been thinking of doing. Plus I learn best diving into whatever it is I am doing.

I am making a basic numbers generator for U.S. lottery called PowerBall. I have practiced working with jQuery before, but this I want to make from strait JavaScript (no libraries involved).

The generator is pretty simple really,5 white balls are picked at random ranging 01-59, as well as a red ‘poweball’ ranging 01-35.

I have gotten to the point where I have the basic HTML template and a button that runs a function on an onclick, but I can only get one result for the white ball #.

I have not looked at the PowerBall value yet as I figure it should be easy for one value and .push() the key to the assigned array.

I have only been working on the five numbers and I think of multiple ways it could be solved.I want to keep DRY (Don’t Reduplicate Yourself) and DIE (Duplication Is Evil) to a minimum. Unfortunately, I haven’t even got a repetitive block of js to work:

I did some further research and I found a blog about generator functions? I tried but no luck:
var whiteBallArray = [i,i,i,i,i];
var i = 0;
(function(whiteBallArray) {
return function() {
i = Math.floor(Math.random() * 59 + 1);
}
})(i);

I hope I gave enough information on here for the problem. Here is a pastebin link of the entire thing if you want to try and work it out.Find Powerball-js and Powerball-html..... So far I got the for loop to work, but the array shows the same value for all five keys...

I can't accept that algorithm, because wasted calls to Math.random result in an imbalance that compromises the randomness of the result.

Interesting call....

Considering the solution requires six unique numbers (an assumption) and taking into account how effective JavaScript’s random number generator is the algorithm may only deteriorate the effectiveness of the result marginally.

Again I was only putting forward an alternate solution to generate the required set and sorry if that pesky algorithm upsets you so.

Good solutions ideas. Thank you so much! @Logic Ali - I took you example and transcribed it in a simpler context. You gave me a chance to review what I have learned about JavaScript such as the ternary operator and sort(), .push(), and splice() methods. Thank You again. Here is an easier (at least me me) version of it and yes a put a while loop in there instead of another for... because I wanted to look like I knew what I was doing

Good solutions ideas. Thank you so much! @Logic Ali - I took you example and transcribed it in a simpler context. You gave me a chance to review what I have learned about JavaScript such as the ternary operator and sort(), .push(), and splice() methods. Thank You again. Here is an easier (at least me me) version of it and yes a put a while loop in there instead of another for... because I wanted to look like I knew what I was doing

Your code does nothing to prevent duplicates, which you said you wanted to do. Here's a small change to your code that removes duplicates.
Because all values are consecutive, instead of picking from an array of available values, we can test for an existing duplicate and keep moving along to the next available value, starting back at the beginning if needed. Requires support for Array.indexOf:

An other method consists to randomly generate a new rank from the remaining choices (witch numbers are decreasing : 59, 58, 57, 56 and 55). Then we have only (not to count the already made choices) to increase our random rank for the preceding choices with a lower rank ! An object is also usefull to avoid an indexOf witch is recent for arrays (and do not work for IE8).

wow guys. I am impressed. I should have made it clearer at first on the beginning of the thread- I just wanted to concentrate on the JS code not having repetition. In other words, I do not want a new variable for each picked number.

I really do not mind if any number in the array have the same number if it randomly happens to be the case. I have seen the same numbers before on the same ticket in real life.

The way I understand Logic Ali is from the while loop added, I transcribe it as:

Code:

while index array value of ball is not equal to -1 <- when this is true (confusing)
do this:
if ball is less than range, true: Else negative array index of ball add one;
then add this new arrangement to Ab

basically this block checks whether the previous number is not the same as the current number generated.

This would be better in a for loop right, how would that look like? just curious.

007Julien, good code. Basically I’m lost. lol. I have a headache.

I understand the code as the index of the numbers picked from the range are removed on each ball, then instead of say 1 through range || 1 - 59, the next range for the next pick is 1 - 58? I am lost because I just want to pick for the same range regardless if same numbers are picked.

I did get something from your code though, it reminds me of how much I do not like ie, ie all versions.

It is very simple !
For the first choice we choose randomly a rank between 1-59 and choose the corresponding number (for example 17).
Then we choose a rank between the remainder choices which are only 58 (for example 33).

Let me say what you just said 007; the first iteration 17 is taken. Then instead of 1-59 when it randomly picked 17, 33 is picked with a range of 1-58. Each time a number it picked the range of numbers is decreased by 1.

That is not what I want to do. The range for the columns should never change. Yes as a result from that values could be the same and that it fine. In your idea 007 do you know if splicing the value chosen and then adding it back to the end of the values effect is differently? That seems like to much unnecessary work to me.

Sorry, I probably misspoke...
I just made ​​random choices among the remaining numbers !
There is a good way of selecting several things out of a larger group without to renew unsuccessful choices, or to sort a large table (two actions that can become very long). In addition, it's not change the probability distribution.
I just wanted to treat your headaches...