dimanche 15 août 2021

Trying to push a pair of array values into a another array randomly without duplicates javascript

I'm trying to make an memory card game but this part is hard to figure out. I have an array with a deck of cards and I'm trying to select a specific amount cards in pairs randomly from the deck of cards and push them into the gameBoard array but I intitally keep getting duplicate of the pairs. Basically I trying to shuffle cards. I tried using a conditional statement but i dont know if this right way to go about this but it still doesnt work.

javascript

const deckOfCards = [
  'card1',
  'card2',
  'card3',
  'card4',
  'card8',
  'card9',
  'card10',
  'card11',
  'card12',
  'card13',
  'card14',
]


let shuffle = deckOfCards[Math.floor(Math.random() * deckOfCards.length)];
let pair = [shuffle, shuffle]
let len = mode

for (let i = pair; i < deckOfCards.length; i++) {
  if (gameBoard.indexOf(deckOfCards[i]) === -1) {
    gameBoard.push(deckOfCards[pair])
  }
}
console.log(gameBoard)
});
let gameBoard = []



Aucun commentaire:

Enregistrer un commentaire