Javascript 防止jquery中重复值

Javascript 防止jquery中重复值,javascript,jquery,Javascript,Jquery,制作一个游戏,我试图让每个水晶从Math.floor(Math.random())中提取一个随机数。但目标是让每个水晶都是不同的数字。我试着用if语句,说一个晶体不能与另一个晶体相等,但在这些方法上似乎运气不好。我需要让所有的晶体都有自己的功能吗 $(文档).ready(函数(){ //你要猜的数字是可变的 var targetNumber=Math.floor(Math.random()*101)+19; //变量,用于编辑DOM中我们必须猜测的数字 $(“.guess_this_numbe

制作一个游戏,我试图让每个水晶从Math.floor(Math.random())中提取一个随机数。但目标是让每个水晶都是不同的数字。我试着用if语句,说一个晶体不能与另一个晶体相等,但在这些方法上似乎运气不好。我需要让所有的晶体都有自己的功能吗

$(文档).ready(函数(){
//你要猜的数字是可变的
var targetNumber=Math.floor(Math.random()*101)+19;
//变量,用于编辑DOM中我们必须猜测的数字
$(“.guess_this_number”).text(targetNumber);
//我们的数字从零开始
var计数器=0;
//“您的数字”是将的DOM更改为等于添加的任何内容
$(“.userTotal”).text(计数器);
//从零开始的胜利
var=0;
//用wins更新DOM
$(“#wins”)。文本(wins);
//从零开始的损失
风险价值损失=0;
//用丢失更新DOM
美元(“#损失”)。文本(损失);
//计算机生成的数字由数学随机数确定
var targetNumber=Math.floor(Math.random()*101)+19;
$('.guess_this_number')。文本(targetNumber);
//所有晶体的对象数组。将随机数映射到每个晶体
变量编号={
“crystal_1”:Math.floor(Math.random()*11)+1,
“crystal_2”:Math.floor(Math.random()*11)+1,
“crystal_3”:Math.floor(Math.random()*11)+1,
“crystal_4”:Math.floor(Math.random()*11)+1,
};
控制台日志(编号)
//每人
//关键是水晶,价值是数学
$。每个(数字、功能(键、值){
$('.+键)。单击(函数(){
//计数器将从每个数字的值开始增加
计数器+=数值;
//防止同一数字出现两次…也许???
//如果((数字[“水晶1”])===(数字[“水晶2”])|
//(数字[“水晶1”])==(数字[“水晶3”])||
//(数字[“水晶1”])==(数字[“水晶4”])||
//(数字[“水晶2”])==(数字[“水晶3”])||
//(数字[“水晶2”])==(数字[“水晶4”])||
//(数字[“crystal_3”])==(数字[“crystal_4”]){
//不,没有运气
//还是。。。?
如果((数字[“水晶1”])==(数字[“水晶2”])){
(数字[“crystal_2”])=Math.floor(Math.random()*11)+1;
(数字[“水晶1”])!=(数字[“水晶2”]);
};//这里也不走运
//计数器类更新为新编号,
//如果超过目标编号“丢失”,则添加“丢失”,然后重置
$('.userTotal').html(计数器);
如果(计数器>目标编号){
$('#wlMessage').text(“您丢失:(”);
损失++;
$(“#损失”)。文本(损失);
计数器=0;
$(“.userTotal”).text(计数器);
$(“.guess_this_number”).text(Math.floor(Math.random()*101)+19);
}
//计数器类更新为新编号,
//如果用户编号等于目标编号,则为赢,添加赢,然后重置
如果(计数器===目标编号){
$('wlMessage')。文本(“您赢了!!!”;
wins++;
$('wins')。文本(wins);
计数器=0;
$(“.userTotal”).text(计数器);
$(“.guess_this_number”).text(Math.floor(Math.random()*101)+19);
}
});
});
});

水晶游戏
水晶游戏
说明书
lorem ipsum一些说明

匹配号码 你的号码
胜利: 损失:
在我的评论中,我问您是否考虑在发布数字清单供使用之前使用数组来保存数字清单;我给您写了一个快速示例,希望能帮助您或引导您朝正确的方向前进

下面的示例为通过单击按钮生成的编号创建一个库存数组,每次单击都会将编号添加到数组中,并且在执行特定功能之前,将根据数组验证该编号,以显示之前是否已生成该编号

从本质上讲,这段代码基本上是为了在生成它们时识别唯一的数字

请参阅下面的代码:

<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css" />
      <!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" />
      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic|Roboto+Mono:400,500|Material+Icons" />-->

      <title>Ilan's Test</title>
   </head>
   <body>

<div class="container">
    <div class="row">
        <div class='col-sm-12' style="margin-top:10px;">
            <div class="form-group">
                <a class="btn btn-primary" id="clickme" style="color:#FFFFFF;">Generate Number</a>
            </div>
            <div id="results">
            </div>
        </div>
    </div>
</div>

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
      <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>-->

<script type="text/javascript">
    // Declare our global variables
    var numcheck = new Array(); // this array will hold all generated numbers
    var newnum; // this is our new number placeholder
    var exists; // this is our boolean indicator

    // On button click we run this function
    $('#clickme').click(function(){
            // Assign a new random number to our variable
            newnum = Math.floor(Math.random() * 11) + 1;
            // Itirate through our numcheck array and check if the number exists         
            $.each(numcheck, function(i, item) {

                if (item == newnum){
                    // If the number exists, set our indicator to true, exit this loop
                    exists = true;
                    return false;
                } else {
                    // If the number does not exist, set our indicator to true
                    exists = false;
                }
            });

            // Check our indicator what happened when we went through the array, the reason I use indicator is because if I have additional code I need this value in I can use it later
            if (exists == true){
                // If the number exists, indicate in my results log the number has been used
                $('#results').append('<span class="text-danger">' + newnum + ' has already been used.</span><br>');
            } else {
                // If the number does not exist in our array, indicate in the results it's a fresh number and do any custom coding needed in the below clause
                $('#results').append('<span class="text-success">New number generated: ' + newnum + '</span><br>');
            }

            // Add the number to the array regardless if true or false so we can analzye its length in the future if needed, the array is our number inventory which tells us when a number has been previously used
            numcheck.push(newnum);

    });

</script>

   </body>
</html>

宜兰试验
生成编号
//声明我们的全局变量
var numcheck=new Array();//此数组将保存所有生成的数字
var newnum;//这是我们的新数字占位符
var exists;//这是我们的布尔指示符
//单击按钮,我们运行此函数
$('#clickme')。单击(函数(){
//给变量分配一个新的随机数
newnum=Math.floor(Math.random()*11)+1;
//通过我们的numcheck数组初始化并检查该数字是否存在
$.each(numcheck,函数(i,项){
如果(项==newnum){
//如果这个数字存在,将我们的指示器设置为true,退出这个循环
存在=真;
返回false;
}否则{