2017-02-14 12 views
0

ランダムゲームを<li>で行うことはできません。各ゲームはユニークでなければなりません。ランダムでユニークな配列の文字列

var randomGames = ['Life is Strange', 'Titanfall', 'Call of Duty', 'Tales from the Borderlands', 'Assassin\'s Creed', 'Tomb Raider', 'FIFA']
<ul class="list-group"><h4>These are the games that James like the most</h4> 
 
     <li class="james list-group-item">...</li> 
 
     <li class="james list-group-item">...</li> 
 
     <li class="james list-group-item">...</li> 
 
</ul>

+1

何が問題なのですか? – Zac

+0

実際に乱数を割り当てるJSはどこですか? – BenM

+0

@BenM私は作ったが、それは正しくないので、私はそれを投稿していない。 –

答えて

1

は、配列からランダムにアイテムを選択し、現在のliテキストとしてそれを追加し、アレイから削除し返し、その後、配列にこれらのゲームのタイトルを置くことができます。

var randomGames = ['Life is Strange', 'Titanfall', 'Call of Duty', 'Tales from the Borderlands', 'Assassin\'s Creed', 'Tomb Raider', 'FIFA'] 
 
var li = document.querySelectorAll('li') 
 

 
for(var i = 0; i < li.length; i++) { 
 
    var n = parseInt(Math.random(randomGames.length) * randomGames.length) 
 
    li[i].textContent = randomGames[n] 
 
    randomGames.splice(n, 1) 
 
    
 
}
<ul class="list-group"> 
 
    <h4>These are the games that James like the most</h4> 
 
    <li class="james list-group-item">...</li> 
 
    <li class="james list-group-item">...</li> 
 
    <li class="james list-group-item">...</li> 
 
</ul>

+0

ありがとうございました!それは働いている。 –

2

あなたはjQueryでこのような何かを行うことができます。

var randomGames = ['Life is Strange', 'Titanfall', 'Call of Duty', 'Tales from the Borderlands', 'Assassin\'s Creed', 'Tomb Raider', 'FIFA']; 
 

 
var filteredArr = [...new Set(Array.from(randomGames))] 
 

 
for(var i = 0; i < filteredArr.length; i++) { 
 
    $('.list-group').append('<li class="james list-group-item">' + filteredArr[i] + '</li>'); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h4>These are the games that James like the most</h4> 
 
<ul class="list-group"> 
 
</ul>

それとも純粋なJS

var randomGames = ['Life is Strange', 'Titanfall', 'Call of Duty', 'Tales from the Borderlands', 'Assassin\'s Creed', 'Tomb Raider', 'FIFA']; 
 

 
var filteredArr = [...new Set(Array.from(randomGames))] 
 

 
for(var i = 0; i < filteredArr.length; i++) { 
 
    var node = document.createElement("li"); 
 
    var textnode = document.createTextNode(filteredArr[i]); 
 
    node.appendChild(textnode);  
 
    document.getElementById("list-group").appendChild(node); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h4>These are the games that James like the most</h4> 
 
<ul id="list-group"> 
 
</ul>

+0

質問にjQueryタグがありません。なぜjQueryの回答を提供するのですか? – Connum

+0

重複を取り除いていません。「各ゲームは一意でなければなりません。 – topheman

+0

ああ、上のコードは実際に要素をランダム化するのではなく、重複をフィルタリングして動的にプッシュし、順序付けられていないリストを作成します。私はOPを完全に理解していませんでした。 –

0

あなたは

+1

OPには既に配列があります。その答えはどのような助けとなるでしょうか? – Connum

1

これは、配列をランダム化し、重複を除外し、その後、(私はより容易なアクセスのためのIDを与えた)ulに新しいli要素としてゲームを追加します。

var randomGames = ['Life is Strange', 'Titanfall', 'Call of Duty', 'Tales from the Borderlands', 'Assassin\'s Creed', 'Tomb Raider', 'FIFA']; 
 

 
// randomize the array: 
 
// (shuffle function taken from http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) 
 

 
function shuffle(array) { 
 
    var currentIndex = array.length, temporaryValue, randomIndex; 
 

 
    // While there remain elements to shuffle... 
 
    while (0 !== currentIndex) { 
 

 
    // Pick a remaining element... 
 
    randomIndex = Math.floor(Math.random() * currentIndex); 
 
    currentIndex -= 1; 
 

 
    // And swap it with the current element. 
 
    temporaryValue = array[currentIndex]; 
 
    array[currentIndex] = array[randomIndex]; 
 
    array[randomIndex] = temporaryValue; 
 
    } 
 

 
    return array; 
 
} 
 

 
randomGames = shuffle(randomGames); 
 

 
// remove duplicates 
 
randomGames.filter(function (elem, index, self) { 
 
    return elem !== "" && index == self.indexOf(elem) 
 
}); 
 

 
// add the games to the list 
 
var gameslist = document.getElementById('gameslist'); 
 
for (var i = 0; i < randomGames.length; i++) { 
 
    var newLi = document.createElement('li'); 
 
    newLi.className = 'james list-group-item'; 
 
    newLi.textContent = randomGames[i]; 
 
    gameslist.appendChild(newLi); 
 
}
<h4>These are the games that James like the most</h4> 
 
<ul class="list-group" id="gameslist"> 
 
</ul>