I am trying to make a card game simulator, but have problems with my deck array.
let colors = ["heart","diamond","spade","club"];
let numbers = ["ace","2","3","4","5","6","7","8","9","10","jack","queen","king"];
function createDeck(){
let card = {};
let counter = 0;
let cards = [];
for (let i = 0; i<colors.length;i++){
for (let j = 0; j<numbers.length; j++){
card = {
color: colors[i],
number: numbers[j]
}
cards[counter] = card;
counter ++;
}
}
return cards;
}
function shuffle(array){
for(let i = array.length -1; i > 0; i--){
const j = Math.floor(Math.random() * i)
const temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
let deck = createDeck();
console.log(deck);
shuffle(deck);
console.log(deck);
Both of the console logs outputs the shuffled array.
I have tried with another array to test it, and then it works.
let test = [1,2,3]
console.log(test);
shuffle(test);
console.log(test);
This outputs the unshuffled array first, and then the shuffled array.
console.log(JSON.stringify(deck));then it won't be both the same - the console evaluates arrays and objects when you view them in the console ... so, it's not a snapshot as such, unless you make it one ... alternativelyconsole.log(deck.slice())- then you are logging an array that can't changeThis outputs the unshuffled array first, and then the shuffled array.no .. it does not