2016-07-26 43 views
-2

我有兩個不同的陣列,setOnesetTwo。我想創建一個包含10個元素的新數組,但前3個元素應該從第一個數組(setOne)隨機選取,其餘7個將來自setOnesetTwo如何使用從第一個和第二個數組中獲取n值創建新陣列

這裏是數組:

var setOne  = [['12+16','28'], ['15+11','26'], ['16+22','38'], ['13+14','27'], ['15+24','39'], ['14+12','26'], ['10+17','27'], ['11+11','22'], 
 
        ['13+15','28'], ['12+21','33'], ['24+13','37'], ['33+21','54'], ['40+18','58'], ['34+31','65'], ['25+42','67'], ['22+15','37'], 
 
        ['24+12','36'], ['20+15','35'], ['25+14','39'], ['21+21','42'], ['41+25','66'], ['53+24','77'], ['35+31','66'], ['62+37','99'], 
 
        ['54+35','89']]; 
 

 
var setTwo  = [['15+18=','33'], ['17+17=','34'], ['13+19=','32'], ['18+14=','32'], ['15+27=','42'], ['18+17=','35'], ['27+29=','56'], ['23+28=','51'], 
 
        ['36+37=','73'], ['45+25=','70'], ['46+45=','91'], ['38+57=','95'], ['49+43=','92'], ['37+53=','90'], ['48+33=','81']];
我試過下面代碼 -

generateQues: function() { 
 
     //Set position for set Questins 
 
     //quesTextValue = this.add.text(50,400,randQues[0], textStyleQues); 
 
     // Generating random questions from given list of ques - setOne. 
 
    for(var i = 0; i < 3; i++) { 
 

 
     
 
      if(i<=3) { 
 

 
        var ranNum = Math.floor(Math.random() * setOne.length); 
 
        //console.log(ranNum); 
 
       randQues = setOne[ranNum]; 
 

 
        console.log(randQues); 
 
       // quesTextValue = this.add.text(50,400, randQues[0],textStyleQues); 
 

 
      } else { 
 
       var ranNum = Math.floor(Math.random() * setTwo.length); 
 
       var rque = setTwo[ranNum]; 
 
       console.log(rque); 
 
       randQues.push(rque); 
 
       console.log(randQues); 
 

 

 
      } 
 
     } 
 
     
 
    // quesTextValue.destroy(); 
 
    },

請幫我解決這個問題。

+2

沒有按照預期的代碼無法正常工作,您可以在這裏貼了嗎? – zee

+0

是的,我在編輯問題。 –

回答

0

您可以使用一些隨機值並拼接選定的項目以防止結果集中出現重複項。

var setOne = [['12+16', '28'], ['15+11', '26'], ['16+22', '38'], ['13+14', '27'], ['15+24', '39'], ['14+12', '26'], ['10+17', '27'], ['11+11', '22'], ['13+15', '28'], ['12+21', '33'], ['24+13', '37'], ['33+21', '54'], ['40+18', '58'], ['34+31', '65'], ['25+42', '67'], ['22+15', '37'], ['24+12', '36'], ['20+15', '35'], ['25+14', '39'], ['21+21', '42'], ['41+25', '66'], ['53+24', '77'], ['35+31', '66'], ['62+37', '99'], ['54+35', '89']], 
 
    setTwo = [['15+18=', '33'], ['17+17=', '34'], ['13+19=', '32'], ['18+14=', '32'], ['15+27=', '42'], ['18+17=', '35'], ['27+29=', '56'], ['23+28=', '51'], ['36+37=', '73'], ['45+25=', '70'], ['46+45=', '91'], ['38+57=', '95'], ['49+43=', '92'], ['37+53=', '90'], ['48+33=', '81']], 
 
    sets = [setOne.slice(), setTwo.slice()], 
 
    count = [3, 7], 
 
    result = [], 
 
    a, p; 
 

 
while (count[0] + count[1]) { 
 
    p = Math.floor(Math.random() * 2); 
 
    if (count[p]) { 
 
     a = sets[p]; 
 
     result.push(a.splice(Math.floor(Math.random() * a.length)[0], 1)); 
 
     count[p]--; 
 
    } 
 
} 
 

 
console.log(result);

0

你可以做這樣的;前三個是來自第一個數組的唯一隨機選擇,其餘七個是來自兩個數組中唯一的隨機選擇。

var setOne = [['12+16','28'], ['15+11','26'], ['16+22','38'], ['13+14','27'], ['15+24','39'], ['14+12','26'], ['10+17','27'], ['11+11','22'], ['13+15','28'], ['12+21','33'], ['24+13','37'], ['33+21','54'], ['40+18','58'], ['34+31','65'], ['25+42','67'], ['22+15','37'], ['24+12','36'], ['20+15','35'], ['25+14','39'], ['21+21','42'], ['41+25','66'], ['53+24','77'], ['35+31','66'], ['62+37','99'], ['54+35','89']], 
 
    setTwo = [['15+18=','33'], ['17+17=','34'], ['13+19=','32'], ['18+14=','32'], ['15+27=','42'], ['18+17=','35'], ['27+29=','56'], ['23+28=','51'], ['36+37=','73'], ['45+25=','70'], ['46+45=','91'], ['38+57=','95'], ['49+43=','92'], ['37+53=','90'], ['48+33=','81']]; 
 
    s1Copy = setOne.slice(), 
 
    result = []; 
 
for (var i = 0; i < 3; i++) result.push(s1Copy.splice(~~(Math.random()*s1Copy.length),1)[0]); 
 
s1Copy.push(...setTwo); 
 
for (var i = 0; i < 7; i++) result.push(s1Copy.splice(~~(Math.random()*s1Copy.length),1)[0]); 
 
console.log(result);

相關問題