-1

如何检查此脚本中匹配的号码,卡在这里,我需要将用户号码数组与乐透号码数组进行比较,并显示他们正确的号码以及他们的奖金价值。

function numbers() {

    var numbercount = 6;
    var maxnumbers = 40;

    var ok = 1;
    r = new Array(numbercount);
    for (var i = 1; i <= numbercount; i++) {
        r[i] = Math.round(Math.random() * (maxnumbers - 1)) + 1;
    }
    for (var i = numbercount; i >= 1; i--) {
        for (var j = numbercount; j >= 1; j--) {
            if ((i != j) && (r[i] == r[j])) ok = 0;
        }
    }
    if (ok) {
        var output = "";
        for (var k = 1; k <= numbercount; k++) {
            output += r[k] + ", ";
        }
        document.lotto.results.value = output;
    } else numbers();

}

function userNumbers() {
    var usersNumbers = new Array(5);
    for (var count = 0; count <= 5; count++) {
        usersNumbers[count] = window.prompt("Enter your number " + (count + 1) + ": ");
    }
    document.lotto.usersNumbers.value = usersNumbers;
}
4

3 回答 3

1

这是一个乐透号码生成器和一个评分系统。我将把它留给你来验证用户输入。

function lottoGen(){
    var lottoNumbers = [];
    for(var k = 0; k<6; k++){
        var num = Math.floor(Math.random()*41);
        if(lottoNumbers.indexOf(num) != -1){
            lottoNumbers.push(num);
        }
    }
        return lottoNumbers;
}

function scoreIt(){
    var usersNumbers = document.getElementsByName('usersNumbers').item(0);
    usersNumbers = String(usersNumbers)
    usersNumbers = usersNumbers.split(' ');
    var matches = 0;
    for(var i = 0; i<6; i++){
        if(lottoNumbers.indexOf(usersNumbers[i]) != -1){matches++;}
    }
    return matches;
}
于 2013-10-30T19:35:40.047 回答
0

嗨,我是新手,并试图自己学习,所以显然我不是专家,但上面的代码对我来说很有意义,除了我无法让它工作..我试过到console.log,它说RETURN,所以我可以看到数字,但它仍然显示一个空数组。我认为这是因为它在循环之外。我尝试了各种方法,但我得到的最好的方法是一个循环相同数字的数组或一个包含 6 个数字但其中一些重复的数组。

function lottoGen(){
    var lottoNumbers = [];
    for(var k = 0; k<6; k++){
        var num = Math.floor(Math.random()*41);
        if(lottoNumbers.indexOf(num) != -1){
            lottoNumbers.push(num);
        }
    }
        return lottoNumbers;
}
于 2015-02-03T14:05:15.107 回答
0

Lotto JS:CODEPEN 演示 >> 这里 <<

(function(){
  var btn = document.querySelector("button");
  var output = document.querySelector("#result");

  function getRandom(min, max){
      return Math.round(Math.random() * (max - min) + min);
  }

  function showRandomNUmbers(){
      var numbers = [], 
          random;

      for(var i = 0; i < 6; i++){   

        random = getRandom(1, 49);
        while(numbers.indexOf(random) !== -1){
            console.log("upps (" + random + ") it is in already.");
            random = getRandom(1, 49);
            console.log("replaced with: (" + random + ").");
        }

        numbers.push(random);

      }

    output.value = numbers.join(", ");

  }
  btn.onclick = showRandomNUmbers;
})();
于 2016-03-19T02:08:34.613 回答