2013-10-16 22 views
2

編輯:如果你看過馬特科比的答案,你會看到,它應該工作,但他使用的indexOf()方法和方法不能與IE瀏覽器8或更高版本,我需要它來對IE 8的工作我試着這樣做的工作繞到的indexOf()方法,但它不工作。使用Javascript - 生成隨機數對,如果對不存在

var tester = -1; 
for (var test=0; test<xposition.length; test++) { 
    if (x == xposition[0]) { 
     tseter = x; 
    } 
} 

任何想法爲什麼它不工作?

原題: 所以我要生成隨機數對,但只有當尚未生成數量的對。以下是我的嘗試,希望如果你閱讀我所嘗試的內容,你會明白什麼是我需要的。

function randomPairs() { 
    var xposition = []; //array which holds all x coordinates 
    var yposition = []; //array which holds all y coordinates 
    for (var i=0; i<5; i++) { //do everything below 5 times (generate 5 pairs) 

     var x = getRandom(1,7); //generate new x point 
     var y = getRandom(2,7); //generate new y point 

     if (jQuery.inArray(x, xposition)) { //if newly generated x point is already in the xposition array (if it was already previously generated 
      var location = xposition.indexOf(x) //find the index of the existing x 

      if (y == yposition[location]) { //if the newly generated y points equals the same y point in the same location as x, except in the yposition array 

       while (y == yposition[location]) { 
        y = getRandom(2, 7); //change y 
       } 
      } 
     } 
    } 
    xposition.push(x); //put x into the array 
    yposition.push(y); //put y into the array 
} 

那麼,任何想法爲什麼它不工作?我是否正確使用了jQuery.inArray()和.indexOf()方法?

哦,getRandom是

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

基本上,它生成的最小值和最大值之間的一個數。

此外,當我試圖做

alert(xposition); 
alert(yposition); 

是空白。

+0

你能告訴我們你的'getRandom()'函數嗎?爲什麼x和y的參數不同?你只是通過軸的規模,並做一個'Math.random'?只要確定。 – aug

+2

'function randomPairs(){return [4,4]; }'。直接來自http://xkcd.com/221/ –

+0

'jQuery。inArray(x,xposition)'返回元素的索引如果存在,那麼爲什麼你要在這個函數索引裏搜索? – Dvir

回答

3

的問題是,你的循環之外添加xy到陣列。用於此修復程序(加上去除不必要的jQuery)是:

function randomPairs() { 
    var xposition = []; //array which holds all x coordinates 
    var yposition = []; //array which holds all y coordinates 
    for (var i=0; i<5; i++) { //do everything below 5 times (generate 5 pairs) 

     var x = getRandom(1,7); //generate new x point 
     var y = getRandom(2,7); //generate new y point 

     var location = xposition.indexOf(x); 
     if (location > -1) { //if newly generated x point is already in the xposition array (if it was already previously generated 
      if (y == yposition[location]) { //if the newly generated y points equals the same y point in the same location as x, except in the yposition array 
       while (y == yposition[location]) { 
        y = getRandom(2, 7); //change y 
       } 
      } 
     } 
     xposition.push(x); //put x into the array 
     yposition.push(y); //put y into the array 
    } 
} 

請注意,你應該從這個函數返回的東西。

如果你要支持舊的瀏覽器,與

var location = jQuery.inArray(x, xposition); 
+0

uhm'if(location> -1)做什麼?如果在數組中找不到x,xposition.indexOf(x)會返回負數嗎? – user2719875

+2

如果'x'不在數組中,'indexOf(x)'返回'-1'。否則,它會返回一個索引,該索引必須大於或等於0. –

+0

hm,您是否嘗試過在計算機上提供的代碼?它對你有用嗎? – user2719875

0

一個使用這種方法的主要問題的替代線路

var location = xposition.indexOf(x); 

是,你必須在有多個想病例具有相同x或y值的唯一對。

x = [1, 1, 1], y = [1, 2, 3]

注意僅Array.indexOf返回第一索引時給定元素可以在陣列中找到。所以你必須從你找到匹配的索引開始遞歸運行它。

生成一個唯一的整數對的一種簡單的方法可以在不jQuery的做到: http://jsfiddle.net/WaFqv/

我假設的順序事做,所以x = 4, y = 3 & x = 3, y = 4將被視爲兩個獨特的對。