samedi 24 mars 2018

trying pull generate a random number with conditions; get caught in infinite loop

I am trying to draw a random number where if it is a single digit number, the number appears with a "0" in front like 09, and if it is a double digit number, the first digit can't be higher than 6. For some reason I'm getting stuck in an infinite loop.

var numString = "737328293048463535555282930405857826161711829203049848347362626171192190203948576767574738383920201101010929383484757576765646454535354343434444444444444444444444444444444444444736262796345690602935069823598326590862350968235896235098632596346509"
ranNum = numString.split('')[(Math.floor(Math.random() * numString.length ))];

function megaNum(numString){
    var numType = Math.floor(Math.random() * 6)
    // one out of seven chance to be a single digit number
    if (numType == 0){
        // if single digit number
        do{
        num1 = ranNum
        }
        while(ranNum == 0);
        // keep drawing numbers till ranNum is not 0
        num1 = "0" + num1
        // inserts a "0" in front of ranNum
    }


    if (numType > 0){
        // if double digit number
        firstDigit = ""
        secondDigit = ""
        do{
            firstDigit = ranNum
        }
        while(ranNum >= 7);
        // first digit must be 6 or less

        secondDigit = ranNum
        num1 = firstDigit.concat(secondDigit)

    }
    return (num1)
}




Aucun commentaire:

Enregistrer un commentaire