This will randomize the array and filter out duplicates, then add the games as new li
elements to the ul
(which I gave an id for easier access):
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>