2014-11-06 40 views
0

我正在使用的JavaScript。我试图将随机数设置为5个变量,它们不能相等。虽然循环检查多个变量是相等的

//set correct, dbcount is at 20ish now, will end up in the thousands later 
correct = Math.floor(Math.random()*dbcount); 

//intially set wrong variables 
wrong1 = correct; 
wrong2 = correct; 
wrong3 = correct; 
wrong4 = correct; 

//set wrong ID not equal to each other or correct 
while(wrong1 === correct && wrong2 === correct && wrong3 === correct && wrong4 === correct && 
     wrong1 === wrong2 && wrong1 === wrong3 && wrong1 === wrong4 && 
     wrong2 === wrong3 && wrong2 === wrong4 && 
     wrong3 === wrong4){ 
      wrong1 = Math.floor(Math.random()*dbcount); 
      wrong2 = Math.floor(Math.random()*dbcount); 
      wrong3 = Math.floor(Math.random()*dbcount); 
      wrong4 = Math.floor(Math.random()*dbcount); 
} 

每隔一段时间,会有一些相同的东西。我做错了吗?我应该使用嵌套if语句吗?需要的东西,将几乎做这样的事情:

while(a == b == c == x == y){ 
    //then do stuff until a, b, c, x, y don't equal each other. 
} 

谢谢

+1

你或许应该使用数组,而不是一堆的变量。 – elclanrs 2014-11-06 07:56:41

+0

不==假设遵循传递属性? a == b,b == c然后a == c? – SMA 2014-11-06 07:59:00

+0

只要在循环中生成随机数,并且每次生成上次迭代的随机数添加数时。这样,所有数字都不会相同。 – 2014-11-06 08:01:17

回答

0

因为你的价值观是数字,你可以通过做在对象上使用标志属性。首先让我们用离散变量来做,然后回来看看用wrong数组来代替它:

带离散变量:(编辑:我真的不喜欢这种方法,强烈建议跳转到下一个代替)

var correct, wrong1, wrong2, wrong3, wrong4, flags, foundDuplicate, dbcount; 
 

 
dbcount = 20; 
 

 
//set correct, dbcount is at 20ish now, will end up in the thousands later 
 
correct = Math.floor(Math.random()*dbcount); 
 

 
do { 
 
    wrong1 = Math.floor(Math.random()*dbcount); 
 
    wrong2 = Math.floor(Math.random()*dbcount); 
 
    wrong3 = Math.floor(Math.random()*dbcount); 
 
    wrong4 = Math.floor(Math.random()*dbcount); 
 
    flags = {}; 
 
    foundDuplicate = [correct, wrong1, wrong2, wrong3, wrong4].some(function(val) { 
 
     // Have we seen this value before? 
 
     if (flags[val]) { 
 
      // Yes, we have a duplicate 
 
      return true; 
 
     } 
 

 
     // No, remember that we've seen it and continue 
 
     flags[val] = true; 
 
     return false; 
 
    }); 
 
} 
 
while (foundDuplicate); 
 
snippet.log("correct: " + correct); 
 
snippet.log("wrong1: " + wrong1); 
 
snippet.log("wrong2: " + wrong2); 
 
snippet.log("wrong3: " + wrong3); 
 
snippet.log("wrong4: " + wrong4);
<!-- Temporary snippet object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这工作,因为该号码来获得转换为字符串,并将其用作属性名称。 Array#some返回true第一次调用迭代函数时返回真值,如果没有调用迭代函数,则返回false

随着wrong阵列代替,其具有不依赖于转换的数字为字符串的优点:

var correct, wrong, newValue, duplicate, dbcount; 
 

 
dbcount = 20; 
 

 
//set correct, dbcount is at 20ish now, will end up in the thousands later 
 
correct = Math.floor(Math.random()*dbcount); 
 

 
wrong = []; 
 
while (wrong.length < 4) { 
 
    do { 
 
     newValue = Math.floor(Math.random()*dbcount); 
 
     duplicate = newValue === correct || wrong.some(function(val) { 
 
      return val === newValue; 
 
     }); 
 
    } 
 
    while (duplicate); 
 
    wrong.push(newValue); 
 
} 
 
snippet.log("correct: " + correct); 
 
snippet.log("wrong: " + wrong.join(", "));
<!-- Temporary snippet object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

,做的检查是在do..while环路的一部分,所以让我们挑选一下:

  1. 我们以中的一个值开始和一个空的wrong阵列。

  2. 我们的外部while循环,直到我们在wrong有四个条目。

  3. 在里面,我们输入do...while循环。

  4. 我们选择一个新值,我们想要添加到wrong阵列。

  5. 然后我们有这样的:

    duplicate = newValue === correct || wrong.some(function(val) { 
        return val === newValue; 
    }); 
    

    那台duplicate标志true如果我们的newValuecorrectwrong任何当前值,false重复的,如果它不是,做这样的:

    • 如果newValue === correctduplicate设置为true和wrong.some功能是从来没有CA lled   —我们已经知道这个值是重复的。

    • 否则,我们呼叫wrong.some,它将查看wrong中的条目,调用我们为每个值给出的回调(迭代器)函数。我们的迭代器函数检查给定的每个值(wrong阵列中的现有值,每次一个)并检查它们是否匹配newValue。如果是这样,迭代函数返回true和停止wrong.some“循环”,使wrong.some返回truetrue =是的,有一个重复的值)。如果wrong.some到达阵列的末端,我们的迭代函数永远不会返回truewrong.some返回falsefalse =没有重复)。

  6. 我们保持do...while内循环,直到我们找到一个值,是不是重复。

  7. 一旦我们离开了do...while,我们知道newValue不是重复的,所以我们将其添加到wrong

  8. 我们的外部while循环继续,直到wrong有四个条目。


(注意:Array#some是ES5一些很旧的JavaScript引擎功能,没有找到。它可以匀/ polyfilled。)

+0

opps在我的浏览器中刷新问题。对于那个很抱歉。 – thomas 2014-11-06 08:22:51

+0

@thomas:'Array#some'是一个方便的实用函数:它为数组中的每个值调用一次你给它的函数('function(val)'),并将值作为第一个参数传递给该函数,当函数返回一个真值(在这种情况下它返回'true')或当它用完数组中的条目(在这种情况下它返回'false')时停止。所以这是一种方便的方法来测试数组中的每个值以查看它是否匹配某个条件(在这种情况下,它是否匹配“正确”或我们其他任何错误值)。 – 2014-11-06 08:23:24

+0

@thomas:'flags'只需要第一个片段:我们为每个我们已经看到的值设置了一个标志属性,所以我们知道它是否是重复的。我会使用第二个片段而不是离散变量。 – 2014-11-06 08:24:35

0

你想要的是像下面

while(true){ 
    if(wrong1 === correct && wrong2 === correct && wrong3 === correct && wrong4 === correct && 
     wrong1 === wrong2 && wrong1 === wrong3 && wrong1 === wrong4 && 
     wrong2 === wrong3 && wrong2 === wrong4 && 
     wrong3 === wrong4){ 
      wrong1 = Math.floor(Math.random()*dbcount); 
      wrong2 = Math.floor(Math.random()*dbcount); 
      wrong3 = Math.floor(Math.random()*dbcount); 
      wrong4 = Math.floor(Math.random()*dbcount); 
    } 
    else break; 
}