在javascript

时间:2017-07-19 18:02:34

标签: javascript arrays

我正在开展一个小项目并试图解决这个问题。以下是我的代码。显然这不是重复的问题。我正在努力解决这个问题。有什么建议吗?

当我说重复时,我的意思是Joe无法与Mark配对,那么Matt和Joe也是一对。

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

for (i = 0; i < 5; i++) { 
 var pick1 = Math.floor(Math.random()*11);
 var pick2 = Math.floor(Math.random()*11);

  while (pick1 === pick2) {
    pick2 = Math.floor(Math.random()*11);
  }
  console.log(people[pick1] + " and " + people[pick2] + " are a 
  group!");
} 

5 个答案:

答案 0 :(得分:0)

看起来您已经在检查重复项,但我个人不会使用while循环。我会把for循环放在一个函数中并在有重复的情况下调用函数,如下所示:

var people = [&#34; Joe&#34;,&#34; Amy&#34;,&#34; Garrett&#34;,&#34; Mark&#34;,&#34; Matt&#34 ;,&#34; Bri&#34;,&#34; Rithy&#34;,&#34; Rob&#34;,&#34; Sandro&#34;,&#34; Sharmila&#34;];

var pickTwo = function() {

for (i = 0; i < 5; i++) { 
 var pick1 = Math.floor(Math.random()*11);
 var pick2 = Math.floor(Math.random()*11);

  if (pick1 === pick2) {
    pickTwo();
  } else {
  console.log(people[pick1] + " and " + people[pick2] +  "are a group!");
} 
}
}

pickTwo();

这样,该函数将继续拾取,直到它得到一对没有重复的东西。这可能导致函数被多次调用,但是它会持续超长时间的可能性很低。

答案 1 :(得分:0)

尝试使用此代码段,它可以确保独特的合作伙伴。

&#13;
&#13;
  var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

    for (i = 0; i < people.length/2; i++) { 

      alert(people.splice(Math.floor(Math.random()*people.length),1) + " and " + people.splice(Math.floor(Math.random()*people.length),1) + " are a group!");
    } 
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我不明白您的意思是避免重复,因为您的代码无法获得pick1pick2的重复条目。

但我可以建议在初始化do..while变量后使用while循环而不是pick2循环,您的代码将是这样的:

  var pick2;
  do {
    pick2 = Math.floor(Math.random() * 10);
  } while (pick1 === pick2);

<强>演示:

&#13;
&#13;
var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

for (i = 0; i < 5; i++) {
  var pick1 = Math.floor(Math.random() * 10);
  var pick2;
  do {
    pick2 = Math.floor(Math.random() * 10);
  } while (pick1 === pick2);
  console.log(people[pick1] + " and " + people[pick2] + " are a group!");
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我的方法是从阵列中删除你使用过的人,以防止他们再次出现,如下所示:

&#13;
&#13;
var people = [
	"Joe",
    "Amy",
    "Garrett",
    "Mark",
    "Matt",
    "Bri",
    "Rithy",
    "Rob",
    "Sandro",
    "Sharmila"
 ];

var num_people = people.length;
rst = {};

while ( num_people > 1 ){
	[1,2].forEach( function( num ){
    	var index =  Math.floor(Math.random() * num_people);
    	rst[ num ] = people[ index ];
        people.splice( index, 1 );
    	num_people--;
    });
	console.log(rst[ 1 ] + " and " + rst[ 2 ] + " are a group!");
}
&#13;
&#13;
&#13;

答案 4 :(得分:0)

Shuffle数组,然后拆分为对:

var people = ["Joe", "Amy", "Garrett", "Mark", "Matt", "Bri", "Rithy", "Rob", "Sandro", "Sharmila"];

// returns a new shuffled array
function shuffle(array) {
  const arr = [...array];
  let m = arr.length, i;
  
  while (m) {
    i = (Math.random() * m--) >>> 0;
    [arr[m], arr[i]] = [arr[i], arr[m]]
  }
  return arr;
}

// split into arrays of partners
function chunk2(array) {
  let pairs = [];
  for(let i = 0; i < array.length; i += 2) {
    pairs.push([array[i], array[i + 1]]); // you can console.log here instead
  }
  return pairs;
}

console.log(chunk2(shuffle(people))); // shuffle and split to pairs