I am making a matching game for a group project in a JavaScript Jquery class. I have 36 images that I can use for the tiles. I want the game to randomly pick 18 of these and put them into a new array. I also need these 18 numbers in the new array twice, because that new array is going to be shuffled and then used the build the game board.
Here is my code:
let objects = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29','30', '31', '32', '33', '34', '35', '36'],
function cardList () {
var cardList = [36];
for (i = 0; i < cardList.length; i+=2) {
cardList[i] = objects[Math.floor(Math.round((Math.random()*36)+1))];
cardList[i+1] = cardList[i];
}
return cardList;
}
I tried to use the Chrome debugger on this and it this isn't creating all the numbers, it's just creating the two initial numbers.
There has got to be a better way to do this, I have looked at several examples of a matching game and most of those have the exact number of cards needs, which may be causing me problems.