samedi 24 mars 2018

in js, generating a powerball ticket, need no repeat numbers

Here is my code for generating a powerball ticket based off a set string of numbers that will be entered by the user in an app.

num1 = ""
num2 = ""
num3 = ""
num4 = ""
num5 = ""
num6 = ""

var numString = "1325102719740829201002101968241083020081601103414208523811"
function megaNum(numString){
    numType1 = [(Math.floor(Math.random() * 6 ))];
    if (numType1 == 0){
        do{
         num1 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num1 == 0);
        num1 = "0" + num1

    }

    if (numType1 != 0){
        do{
            digit1 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit1 > 6 || digit1 == 0);
        digit2 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        num1 = digit1.concat(digit2)
    }
    numType2 = [(Math.floor(Math.random() * 6 ))];
    if (numType2 == 0){
        do{
         num2 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num2 == 0);
        num2 = "0" + num2

    }

    if (numType2 != 0){
        do{
            digit3 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit3 > 6 || digit3 == 0);
        digit4 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        num2 = digit3.concat(digit4)
    }

    numType3 = [(Math.floor(Math.random() * 6 ))];
    if (numType3 == 0){
        do{
         num3 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num3 == 0);
        num3 = "0" + num3

    }

    if (numType3 != 0){
        do{
            digit5 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit5 > 6 || digit5 == 0);
        digit6 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        num3 = digit5.concat(digit6)
    }
    numType4 = [(Math.floor(Math.random() * 6 ))];
    if (numType4 == 0){
        do{
         num4 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num4 == 0);
        num4 = "0" + num4

    }

    if (numType4 != 0){
        do{
            digit7 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit7 > 6 || digit7 == 0);
        digit8 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        num4 = digit7.concat(digit8)
    }
    numType5 = [(Math.floor(Math.random() * 6 ))];
    if (numType5 == 0){
        do{
         num5 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num5 == 0);
        num5 = "0" + num5

    }

    if (numType5 != 0){
        do{
            digit9 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit9 > 6 || digit9 == 0);
        digit10 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        num5 = digit9.concat(digit10)
    }
    numType6 = [(Math.floor(Math.random() * 6 ))];
    if (numType6 == 0){
        do{
         num6 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (num6 == 0);
        num6 = "0" + num6

    }

    if (numType6 != 0){
        do{
            digit11 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit11 > 2 || digit11 == 0);
        do{
        digit12 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
        }
        while (digit12 > 6);
        num6 = digit11.concat(digit12)
    }
    return (num1 + "-" + num2 + "-" + num3 + "-" + num4 + "-" + num5 + "-" + num6)


}

My question is how do I set the condition that numbers 1 - 5 must be unique - can't repeat? I tried using a while loop but got stuck in an infinite loop. Ive tried a bunch of different ways but can't get it to come out right. I know I need an if statement of some kind but not sure of the syntax that would work to make sure the first five numbers were unique.




Aucun commentaire:

Enregistrer un commentaire