Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div id="word">
  <div class="letter">a</div>
  <div class="letter">t</div>
  <div class="letter">o</div>
  <div class="letter">m</div>

  <label>Enter your guess</label>
  <input type="text" name="guess">

<table id="scoreboard">
    <th>Incorrect Guesses</th>
    <th>Guesses Remaining</th>

# Word-guessing game

This exercise creates a game that lets a player guess letters in a word. When the game starts, the `div#word` should be populated with `div.letter` tiles, one for each letter in the target word. As a player makes correct guesses, the tiles in the target word should be updated to show the correct guesses. As a player makes incorrect guesses, the scoreboard should be updated to show each incorrect guess; also, the number of guesses remaining should be decremented. After seven incorrect guesses, the player loses. The player wins by guessing all of the letters in the word before running out of guesses.

If you're just getting started with JavaScript and DOM manipulation, the instructions below will guide you. If you're more experienced and interested in designing the overall solution from scratch, skip the directions below and get started; you can delete all the function stubs in the JS, too.

## Steps

- Write a function `chooseWord` that returns a random word from the `words` array.
- Write a function `createWordTiles` that takes a string as its argument, creates the `div.letter` tiles for each letter in the word, and places the tiles in `div#word`.
- Write a function `validate` that takes a string as its argument, and returns `true` if the guess is exactly one letter, and false otherwise.
- Write a function `testGuess` that takes a single-letter string as its argument. If the letter is in the target word, return an array containing the index (or indices) where the letter appears in the word. If the letter is not in the target word, return an empty array.
- Write a function `updateWordTiles` that takes an array of indices and updates the DOM to show the letter tiles at the specified indices.
- Write a function `updateScoreboard` that receives two arguments: the guess, and the number of incorrect guesses so far; it should the Incorrect Guesses and Guesses Remaining portion of the scoreboard.
- Write a function `listen` that listens for user input, then handles it using the functions you have created.
- Write a function `init` that uses the other functions you have created to set up and play the game.
              body {
  font-family: Helvetica;

table {
  border-collapse: collapse

table td, table th {
  border: 1px solid black;
  text-align: left;
  padding: 1em;

form {
  margin: 1em 0;

.letter {
  display: inline-block;
  border: 1px solid black;
  padding: 1em 1.2em;
  height: 1.2em;
              let words = window.words;
let gameState = {
  incorrectGuesses : [],
  guessesRemaining : 7

function chooseWord () {

function createWordTiles (word) {

function validate (guess) {

function testGuess (guess) {

function updateWordTiles (indices) {

function listen () {

function init () {
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.