2

1 of spades,1 of diamonds,1 of clubs,1 of hearts,2 of spades,2 of diamonds,2 of clubs,2 of hearts,3 of spades,3 of diamonds,3 of clubs,3 of hearts,4 of spades,4 of diamonds,4 of clubs,4 of hearts,5 of spades,5 of diamonds,5 of clubs,5 of hearts,6 of spades,6 of diamonds,6 of clubs,6 of hearts,7 of spades,7 of diamonds,7 of clubs,7 of hearts,8 of spades,8 of diamonds,8 of clubs,8 of hearts,9 of spades,9 of diamonds,9 of clubs,9 of hearts,10 of spades,10 of diamonds,10 of clubs,10 of hearts

,,,

[This is what the program returns]

I'm trying to make a program in JavaScript that allows someone to play poker. I am using the ProcessingJS terminal in Khan Academy. Below is my full program so far. What it's supposed to do is make an array called deck which includes the names of all the cards (not including the face cards) in a deck of cards. That part of the program works. The next part attempts to make a new array called current that is an exact copy of deck. It then tries to print out current, and it does so successfully.

The last for loop is what is causing the problem. It tries to take a random card from current and copy it to another array called player which is supposed to be the player's hand. It then tries to remove that card from the array current.

However, when it tries to print out the array player, all it prints is three commas. I have no idea what the issue is and I have looked at many websites that talk about push and splice. I really have no idea what is wrong.

Again, I want the program to display the player's hand. Thank you for your help.

var deck = [];
var current = [];
var enemy = [];
var player = [];
var suits = ['spades', 'diamonds', 'clubs', 'hearts'];

for (var i = 1; i < 11; i++) {
    for (var x = 0; x < 4; x++) {
        if (i <= 10) {
            deck.push(i + ' of ' + suits[x]);
        }
    }
}

for (var c = 0; c < 40; c++) {
    current[c] = deck[c];
}

println(current);
var y;

for (var a = 0; a < 4; a++) {
    y = random(0, (current.length - 1));
    player.push(current[y]);
    current.splice(y);
}

println(player);
    

1 Answer 1

1

Why not just simplify this without using splice?

From what I understand, you're drawing the card using random. So, just push that card to the players hand with player.push(current[y]).

Also, when you call current.splice(y), I don't think you're deleting just the card that was drawn. That deletes every card in the deck after the index y. Change this to current.splice(y, 1).

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

4 Comments

Your change to current.splice(y, 1) worked, but I'm still seeing issues with displaying the players hand when I change to player.push(current[y]). The commas are still there. Nevertheless, thanks for your help!
Try this for debugging - print "y" after each iteration and see what number it is, as well as print current[y] and player after each addition.
Found the issue -- it was returning decimal values for y. Arrays don't have decimal indexes. Thank you!
I figured - math.random() generally returns 0 to 1, so I was wondering if you were using a custom implementation or something like that. Glad you found the issue - great job.

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.