I have an array of card names like this:
var deckNames = [ "unused",
"sA", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "sJ", "sQ", "sK",
"hA", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "hJ", "hQ", "hK",
"cA", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "cJ", "cQ", "cK",
"dA", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "dJ", "dQ", "dK",
];
And I want to write a function that randomly selects a card from the array and also removes it from the "deckNames" array above. I have written the following, but it does not seem to be working.
var deal = function(){
var card = Math.floor(Math.random() * 52) + 1;
return deckNames[card];
deckNames.splice(card,1);
};
When I run the deal
function in the console, it randomly picks and returns a card from the array, but the deckNames
array itself does not get the dealt card removed from the array. How can I achieve this? Thank you.
I have an array of card names like this:
var deckNames = [ "unused",
"sA", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "sJ", "sQ", "sK",
"hA", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "hJ", "hQ", "hK",
"cA", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "cJ", "cQ", "cK",
"dA", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "dJ", "dQ", "dK",
];
And I want to write a function that randomly selects a card from the array and also removes it from the "deckNames" array above. I have written the following, but it does not seem to be working.
var deal = function(){
var card = Math.floor(Math.random() * 52) + 1;
return deckNames[card];
deckNames.splice(card,1);
};
When I run the deal
function in the console, it randomly picks and returns a card from the array, but the deckNames
array itself does not get the dealt card removed from the array. How can I achieve this? Thank you.
- 2 You return the card before you do the splice... – Robin Mackenzie Commented Oct 10, 2016 at 22:50
3 Answers
Reset to default 3Your return
statement ends your function before the deck is modified. Switch the statements around so that the return
is the last thing in the function. Additionally, as @DavidE points out, you can't get the card from the array after it's already been removed, so you have to retrieve it before you remove it:
var deal = function(){
var index = Math.floor(Math.random() * 52) + 1;
var card = deckNames[index];
deckNames.splice(index, 1);
return card;
};
Or simply:
var deal = function(){
var card = Math.floor(Math.random() * 52) + 1;
return deckNames.splice(card, 1)[0];
};
(since splice
returns the removed element, wrapped in a new array).
Some other things to consider:
Array indices start at 0, so chances are you don't want the
+1
in your random number generator. You actually want numbers from 0 to 51*:var card = Math.floor(Math.random() * 52);
Every time you deal a card the size of the deck decreases. Instead of generating random numbers up to 51 each time, base that number on the size of the deck when the function is called. Otherwise you'll get index out of bound errors. See below.
Ultimately, this gives you something like this:
var deal = function(){
var card = Math.floor(Math.random() * deckNames.length);
return deckNames.splice(card, 1)[0];
};
It doesn't splice because the splice
call is dead-code; meaning it's impossible for it to ever run.
Once you return
, you "exit" the function. You need to splice before returning:
var deal = function(){
var card = Math.floor(Math.random() * 52) + 1;
deckNames.splice(card,1);
return deckNames[card];
};
Consider:
var test = function(){
console.log("Hello");
return;
console.log("World");
};
test();
What do you think that prints?
Edit:
On rereading your code again, I realized there is still a major issue that will pop up if you deal enough cards. You have 52
hard-coded into your function, but you're removing a card each call. What happens if there are only 10 cards, but the random number generator gives you a 50
? Change the 52
to deckNames.length
to make sure you aren't trying to deal cards that don't exist (which will result in an "array out of bounds" error).
First of all, you can get rid of your "unused"
, then check this out:
function deal(deck){
var card = Math.floor(Math.random()*deck.length); // notice you add the one later - that's not right
return deck.splice(card, 1);
}
var pulledCard = deal(deckNames); // now deckNames array is altered
Now learn this:
function CardGame(deck){
this.deck = deck;
this.deal = function(){
return this.deck.splice(Math.random()*this.deck.length, 1)[0];
}
this.shuffle = function(){{
var l = this.deck.length;
this.deck.sort(function(a, b){
return 0.5 - Math.floor(Math.random()*(l+1))/l;
});
return this.deck;
}
}
var game = new CardGame(deckNames);
var alteredDeck = game.shuffle(); // alteredDeck is same as game.deck;
var singleCard = game.deal(); // game.deck is spliced