Workshop › Klondike Solitaire › Step 5 of 40
A deck of 52
What we are doing
newDeck builds the whole deck: for each of the 4 suits, the 13 ranks. 4 × 13 = 52 cards, all face down.
```js
function newDeck() {
const deck = []
for (let suit = 0; suit < 4; suit++) for (let rank = 1; rank <= 13; rank++) deck.push({ rank, suit, up: false })
return deck
}
```
What it means
const deck = [] starts an empty array.
- The outer
for runs suit 0 to 3; for each, the inner for runs rank 1 to 13 and push adds a card.
{ rank, suit, up: false } is short for { rank: rank, suit: suit, up: false }.
return deck hands the finished deck back to whoever called newDeck().
Your turn
Under the isRed line, leave an empty line and write newDeck. Press Run.
Skills: Arrays and objects, Loops
← Red and black Shuffle →
All steps
- The green table
- Draw one card
- The face of a card
- Red and black
- A deck of 52
- Shuffle
- Piles in one object
- Deal the seven columns
- A place for every pile
- Put the piles on the table
- Fan out the columns
- Outlines for empty places
- Turn over a card
- Space turns a card
- Go through the stock again
- What is under the pointer?
- Hitting a card in a column
- Tap the stock
- Rule 1: building a column
- Rule 2: building a foundation
- Move cards by the rules
- Turn over the uncovered card
- Find where a card goes
- Pick up a card
- Tap to move
- A move counter
- Count the moves
- Where did you grab it?
- Follow the pointer
- Draw the held cards at the pointer
- Drop it where you want
- Drop a king into an empty column
- A clock
- Winning
- You won!
- A new game
- The best time
- Snapshots of the table
- Undo
- Build it yourself: finish by itself