1

Today I wrote my first program in JavaScript all by myself (Yay!). It is a Lotto number picker that randomly picks my six lottery numbers from between 1 and 49. Though it seems to work fine, with no duplicates, I would like the program to print the numbers in numerical order to the alert box. Any help would be appreciated. My code is below:

var firstNumber = Math.round(Math.random() * 49);
var secondNumber = Math.round(Math.random() * 49);
var thirdNumber = Math.round(Math.random() * 49);
var fourthNumber = Math.round(Math.random() * 49);
var fifthNumber = Math.round(Math.random() * 49);
var sixthNumber = Math.round(Math.random() * 49); 


 if(firstNumber === secondNumber || firstNumber === thirdNumber || 
    firstNumber === fourthNumber || firstNumber === fifthNumber ||           
    firstNumber === sixthNumber || firstNumber === 0) {
      Math.round(Math.random() * 49);
 } else if(secondNumber === firstNumber || secondNumber === thirdNumber || 
           secondNumber === fourthNumber || secondNumber === fifthNumber || 
           secondNumber === sixthNumber || secondNumber === 0) {
      Math.round(Math.random() * 49);
} else if (thirdNumber === firstNumber || thirdNumber === secondNumber || 
           thirdNumber === fourthNumber || thirdNumber === fifthNumber || 
           thirdNumber === sixthNumber || thirdNumber === 0) {
      Math.round(Math.random() * 49);
} else if (fourthNumber === firstNumber || fourthNumber === secondNumber || 
           fourthNumber === thirdNumber || fourthNumber === fifthNumber || 
           fourthNumber === sixthNumber || fourthNumber === 0) {
      Math.round(Math.random() * 49);
} else if(fifthNumber === firstNumber || fifthNumber === secondNumber || 
          fifthNumber === thirdNumber || fifthNumber === fourthNumber || 
          fifthNumber === sixthNumber || fifthNumber === 0){
      Math.round(Math.random() * 49);
} else if(sixthNumber === firstNumber || sixthNumber === secondNumber || 
          sixthNumber === thirdNumber || sixthNumber === fourthNumber || 
          sixthNumber === fifthNumber || sixthNumber === 0) {
      Math.round(Math.random() * 49);
} else {
      alert(firstNumber + ", " + secondNumber + ", " + thirdNumber + ", " + 
      fourthNumber + ", " + fifthNumber + ", " + sixthNumber);
}
John Vale
  • 11
  • 5
  • that is massively inefficient. why don't you just fill an array with your digits, shuffle the array, then pick off the first `n` digits? All of that pointless `||` and `===` would be reduced to just a few lines of code. – Marc B Jun 24 '15 at 19:45

3 Answers3

0

If you would use an array, generating and shuffling would be easier. Arrays have a built-in method sort that can sort their items. Unfortunately, the items are sorted by their textual representation, so 5 comes after 41. To solve that, you can specify your own compare callback function that compares two items. That comparison is used internally to sort the entire array. Documentation about that process can be found on the Mozilla Developer Network.

var numbers = [];
for (var i = 0; i < 6; i++){
  numbers.push(Math.round(Math.random() * 49));
}

numbers.sort(
   function(a, b) {
     return a - b;
   });

alert(numbers);

However, this code (like your own) can generate duplicates. A better solution would be to take an array with the numbers 1 to 49, shuffle that array, and then pick the first 5 numbers from that array.

Generating an array with numbers 1 to 49 can simply be done using a for loop from like in the snippet above, only you can just push i onto the array instead of generating a number.

Shuffling an array is a bit harder, especially to do it right. Fortunately there is this other question about that subject, which has a couple of great answers with great algorithms for shuffling.

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

There might be a more efficient way to treat this problem. Try it:

var min = 1,
    max = 49,
    total = 6,
    nums = [];

// For [total] times
for(var i=0; i<total; i++){
  // Generate a random number between [min] and [max]
  var num = Math.floor( Math.random() * max + min );
  // If number already there, repeat
  if( nums.indexOf(num) > -1 ){ i--; continue; }
  // Add the number to the array
  nums.push(num);
}

// Sort and show the resulting array, comma separated
alert( nums.sort(sortNumber).join(', ') );

function sortNumber(a,b) {
    return a - b;
}
blex
  • 24,941
  • 5
  • 39
  • 72
0

Very simple.

 var text =""
    for (i=1;i<=7;i++) {
       noppa = Math.round((Math.random() * 38) + 1);
       text = text + " " + noppa;
    }
alex
  • 1
  • 1