1

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 = []
3
  • 1
    So pair will contain a list with 2 same random elements from deckOfCards, right? So what's the purpose of the for loop started with pair? Since pair is an array with 2 elements. Can you clarify what's your expected result? Commented Aug 15, 2021 at 13:15
  • I would suggest you use _.shuffle from underscore library. Commented Aug 15, 2021 at 13:16
  • it's a memory card game so i wanted the pair to run through the loop and output random different pairs without a duplicate Commented Aug 15, 2021 at 13:23

3 Answers 3

2

just a thought: you can move the selected element to the back of the array and then randomly select stuff from a slice of the prior, unchanged array... just like array.slice(0, {a_number_here}.

something like this I guess (I cannot get your code working so this is not tested yet):

let shuffle = deckOfCards[Math.floor(Math.random() * deckOfCards.length - 5)]
name = deckOfCards.splice(shuffle, 1)
deckOfCards.push(name)

by the way if you can use libraries, just go for underscore.js and it would become very simple. just like this:

b = _.shuffle(deckOfCards).slice(0,5);
console.log(b)

you can also take a look at these repositories too (I haven't yet had the chance to take a look at them though but they kinda have a lot of stars so):

Sign up to request clarification or add additional context in comments.

2 Comments

yeah. I know but this is for school so only vanilla javascript... but I will try your orginal idea
ah that is lame... I mean why don't them allow libraries if you are going to use them in future anyway...
0

you can copy existing one to new one ex : const cloneSheepsES6 = [...sheeps]; then you can apply your condition without any duplications

1 Comment

I will look into this
0

This should give you an array of unique pair of cards.

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

const randomArray = [];
let noOfPairs = 6; // No. of pairs required

while (noOfPairs) {
  let randomNumber = Math.floor(Math.random() * deckOfCards.length);
  if (randomArray.includes(randomNumber)) {
    continue;
  }
  randomArray.push(randomNumber);
  noOfPairs--;
}

randomArray.sort(() => 0.5 - Math.random());

const gameBoard = randomArray.map((item) => [
  deckOfCards[item],
  deckOfCards[item],
]);
console.log(gameBoard);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.