2016-06-16 8 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