猿问

从输入中获取值并放入字符串数组乐透游戏

我是 javascript 新手,我正在尝试制作一个彩票游戏,在其中我生成 8 个随机十六进制数字,如 15 4A FF 45 77 C3 EE 7E 并介绍我的数字。我想找到我猜到的数字。我从输入中获取值并将它们放入一个数组并在数字之间插入空格,然后拆分结果。生成的数字相同,但我不知道我没有得到正确的结果。


<section id="section2">

    <h2>Internet Loto</h2>

    <button onclick="genHexString();">Genereaza numere</button>

    <br />

    <button onclick="tryAgain()" ;>Incearca din nou</button>

    <p id="loto"></p>

    <p class="numere">

        <label for="nr1">Nr1:</label>

        <input pattern="[A-Z0-9]{2}" id="nr1">

        <label for="nr2">Nr2:</label>

        <input pattern="[A-Z0-9]{2}" id="nr2">

        <label for="nr3">Nr3:</label>

        <input pattern="[A-Z0-9]{2}" id="nr3">

        <label for="nr4">Nr4:</label>

        <input pattern="[A-Z0-9]{2}" id="nr4">

        <label for="nr5">Nr5:</label>

        <input pattern="[A-Z0-9]{2}" id="nr5">

        <label for="nr6">Nr6:</label>

        <input pattern="[A-Z0-9]{2}" id="nr6">

        <label for="nr7">Nr7:</label>

        <input pattern="[A-Z0-9]{2}" id="nr7">

        <label for="nr8">Nr8:</label>

        <input pattern="[A-Z0-9]{2}" id="nr8">

    </p>

</section>

函数 genHexString() {


var output = "";

for (let i = 0; i < 16; ++i) {

    output += (Math.floor(Math.random() * 16)).toString(16).toUpperCase();

    document.getElementById("loto").innerHTML += '&nbsp';

    if (i % 2 != 0 && i < 15) {

        output += '&nbsp'; // space for display each number as pair of 2 characters

    }

}


var res = output.split(" "); 

document.getElementById("loto").innerHTML = "Winning numbers: " + output;


var input = "";

for (let i = 1; i < 9; i++) {

    var x = "nr" + i;

    input += document.getElementById(x).value + '&nbsp';

}

var res2 = input.split(" ");

document.getElementById("loto").innerHTML += "<br/>Your numbers: " + res2;

var count = 0;

var i, j;

for (i = 0; i < output.length; i++) {

    for (j = 0; j < input.length; j++) {

        if (res[i] == res2[j]) {

            count += 1;

        }

    }

}


document.getElementById("loto").innerHTML += "<br/>Numere ghicite: " + count;

}


繁华开满天机
浏览 176回答 1
1回答

FFIVE

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes包含功能可以派上用场。试试这个function genHexString() {var output = "";for (let i = 0; i < 16; ++i) {&nbsp; &nbsp; output += (Math.floor(Math.random() * 16)).toString(16).toUpperCase();&nbsp; &nbsp; document.getElementById("loto").innerHTML += '&nbsp';&nbsp; &nbsp; if (i % 2 != 0 && i < 15) {&nbsp; &nbsp; &nbsp; &nbsp; output += '&nbsp'; // space for display each number as pair of 2 characters&nbsp; &nbsp; }}document.getElementById("loto").innerHTML = "Winning numbers: " + output;var count = 0;var input = "";console.clear();for (let i = 1; i <= 8; i++) {&nbsp; &nbsp; var x = `nr${i}`;&nbsp; &nbsp; let value=document.getElementById(x).value;&nbsp; &nbsp; if(output.includes(value) && value)&nbsp; &nbsp; &nbsp; &nbsp;count++;&nbsp; &nbsp; input += value + '&nbsp';}document.getElementById("loto").innerHTML += "<br/>Your numbers: " + input;document.getElementById("loto").innerHTML += "<br/>Numere ghicite: " + count;}label{margin-left:10px;}input{margin-top:5px;margin-left:30px;}.as-console-wrapper { max-height: 100% !important; top: 0; }<section id="section2">&nbsp; &nbsp; <h2>Internet Loto</h2>&nbsp; &nbsp; <button onclick="genHexString();">Genereaza numere</button>&nbsp; &nbsp; <br />&nbsp; &nbsp; <button onclick="tryAgain()" ;>Incearca din nou</button>&nbsp; &nbsp; <p id="loto"></p>&nbsp; &nbsp; <p class="numere">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr1">Nr1:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr1">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr2">Nr2:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr2">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr3">Nr3:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr3">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr4">Nr4:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr4">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr5">Nr5:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr5">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr6">Nr6:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr6">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr7">Nr7:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr7">&nbsp; &nbsp; &nbsp; &nbsp; <label for="nr8">Nr8:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input pattern="[A-Z0-9]{2}" id="nr8">&nbsp; &nbsp; </p></section>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答