JS Project help needed: duplicate, unordered numbers

Project Name: Mega Billions( Clone of Megamillions )

Project Link: https://megabillions.ga/

Source-code link: https://github.com/ryanbest99/ryanbest99.github.io/tree/master/mega-billions

[Problems I want to solve]

  1. Each white ball number MUST be unique (no duplicates)
  2. List the numbers in order…


Hey guys, I’m trying to make a mega-million clone web service using JS.

To pick the random number for each ball, I used ‘Math.random()’ on each ball. (White balls: 1-70 / Yellow-ball: 1-26)

However, it causes two issues that I don’t know how to solve.

  1. Currently, the numbers can be duplicated ( like 26, 26, 26, 26, 27, 21)
  2. The numbers are not in numerical order.

Thank you in advance for your help!

You need to rethink your approach. Start by thinking about the actual physical steps of selecting just 3 balls from the numbers 1-5. Everytime you select a ball, you must remove it from the possible set of balls. This means you can not just pick random numbers from a to b. You could use an array to store the ball numbers in and then pick a random index. Next you would remove that number (based on its index) from the array and repeat until you have all the balls you need.

1 Like

Problem number 2 is easily solved with Array.sort().

For problem number 1, what I’d do is I’d put all the possible numbers in an array, shuffle the array then take the number of items you need from the start of the array. Unfortunately Array doesn’t come with a shuffle function so you’ll have to write it yourself: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Note that it’s easy to make it non-random if you don’t implement it exactly.


Thank you for suggesting a new solution. I need to study Array.sort() and try it. Thx again!

@RandellDawson @sfiquet

Guys, thanks to you, I finally did it !!!
Thanks again for your help!!!

Complete project link: https://megabillions.ga/

Source-code link: