2016-06-16 9 views
0

ランダムな国旗を並べて表示しようとしていますが、一致しないようにする必要がありますが、私の場合は配列に関するソリューションに特化しています。ランダムに一致する配列がありません

function flags() { 
var flagurls = ["ZPlo8tpmp/chi","cJBo8tpk6/sov","QyLo8tpkm/rus","68so8tpl4/pol","x1Ro8tplj/nor","TT3o8tplv/lit","IsZo8tpma/isr"]; 
var randomize = 'url("http://static.tumblr.com/zicio7x/'+flagurls[Math.floor(Math.random()*flagurls.length)]+'.png")'; 
var randomize2 = 'url("http://static.tumblr.com/zicio7x/'+flagurls[Math.floor(Math.random()*flagurls.length)]+'.png")'; 
document.getElementById("th1").style.backgroundImage = randomize; 
document.getElementById("th2").style.backgroundImage = randomize2; 

答えて

0

使用whileループ:

function flags() { 
    var flagurls = ["ZPlo8tpmp/chi","cJBo8tpk6/sov","QyLo8tpkm/rus","68so8tpl4/pol","x1Ro8tplj/nor","TT3o8tplv/lit","IsZo8tpma/isr"]; 
    var randomize = 'url("http://static.tumblr.com/zicio7x/'+flagurls[Math.floor(Math.random()*flagurls.length)]+'.png")'; 
    var randomize2 = randomize; 

    while (randomize2 === randomize) { 
    randomize2 = 'url("http://static.tumblr.com/zicio7x/'+flagurls[Math.floor(Math.random()*flagurls.length)]+'.png")'; 
    } 

    document.getElementById("th1").style.backgroundImage = randomize; 
    document.getElementById("th2").style.backgroundImage = randomize2; 
} 

JSFiddle example

関連する問題