Error Message: "Keep going, you're close!" – Code Seems Correct
Error Message: "Keep going, you're close!" – Code Seems Correct
Hello,
I’m trying to write a function in R to shuffle a deck of cards and deal a specific number of cards. My code looks correct and is very similar to other solutions I’ve seen, but when I run it, I get the error message:
"Keep going, you're close!"
Here is my code:
# Step 1: Define the shuffle_deck function
shuffle_deck <- function(deck) {
shuffled_deck <- sample(deck, length(deck), replace = FALSE)
return(shuffled_deck)
}
# Step 2: Define the deal_hand function
deal_hand <- function(deck = 1:10, num_cards = 3) {
# Shuffle the deck by calling shuffle_deck
shuffled_deck <- shuffle_deck(deck)
# Extract the top num_cards from the shuffled deck
hand <- shuffled_deck[1:num_cards]
# Return the hand
return(hand)
}
# Step 3: Test the deal_hand function
default_hand <- deal_hand() # Should use default deck 1:10 and deal 3 cards
print(default_hand)
custom_hand <- deal_hand(c("Ace", "King", "Queen", "Jack", "10"), 2) # Should deal 2 cards from custom deck
print(custom_hand)