9.2: Card Deck Generation with Loops

Learning Objectives

By the end of this lesson, you should be able to:

  • Create a representation of a standard deck of cards in JavaScript, where each card is an Object, and the deck is an Array of Objects, using a loop.

Introduction

At the end of Module 9.1: JavaScript Objects we shared a hard-coded card deck. This would work for our applications, but we may wish to use loops to generate this deck to be more concise and so we can more easily modify the deck if we need to. Creating the card deck with loops is also a useful exercise in identifying patterns and automating them with loops.

Card Deck Generation Code

We define a makeDeck function that returns a generated standard 52-card deck. Read inline comments to understand the mechanics of this function.

Exercises

Follow Along

Implement a card deck generation function from scratch. What might you do differently?

Last updated