Hello again,I'm writing a blackjack game for class,it generates two random numbers that select from a switch statement that creates a string file name of a card e.g "AceDiamonds.gif", it than fills an array deck[] with these strings,I want to fill the array with the full deck of 52 cards,and not have any duplicates, how do I check the array for an existing element remove it and replace with another card not in the deck??
Here is the code,
<script>
var dealer_hand = new Array();
var player_hand = new Array();
var deck = [];
function cardToString(value,suit)
{
var cvalue="";
var csuit="";
switch(value)
{
case 1:
cvalue = "Ace";
break;
case 2:
cvalue = "Two";
break;
case 3:
cvalue = "Three";
break;
case 4:
cvalue = "Four";
break;
case 5:
cvalue = "Five";
break;
case 6:
cvalue = "Six";
break;
case 7:
cvalue = "Seven";
break;
case 8:
cvalue = "Eight";
break;
case 9:
cvalue = "Nine";
break;
case 10:
cvalue = "Ten";
break;
case 11:
cvalue = "Jack";
break;
case 12:
cvalue = "Queen";
break;
case 13:
cvalue = "King";
break;
default:
cvalue=null;
break;
}
switch(suit)
{
case 1:
csuit="Clubs";
break;
case 2:
csuit="Hearts";
break;
case 3:
csuit="Diamonds";
break;
case 4:
csuit="Spades";
break;
default:
csuit=null;
break;
}
if(value==null||suit==null)
return"";
var myCard = cvalue + csuit +".gif";
return myCard;
}//End of cardToString Function
function shuffle()
{
var i=0;
var j=0;
for( i=0;i<52;i++)
{
var num1 = Math.floor(Math.random()* 13)+1;
var num2 = Math.floor(Math.random()* 4)+1;
myCard = cardToString(num1,num2);
deck[i]=myCard;
}
deck.sort();
for(j=1;j<52;j++)
{
while(deck[j] == deck[j-1])
{
var num1 = Math.floor(Math.random()* 13)+1;
var num2 = Math.floor(Math.random()* 4)+1;
myCard = cardToString(num1,num2);
deck[j]=myCard;
}
}
alert(deck);
}
</script>
<input type=button value="Shuffle" onclick="shuffle()"/>
</body>
</html>