cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

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

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

<table id="scoreboard">
  <tr>
    <th>Incorrect Guesses</th>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <th>Guesses Remaining</th>
    <td>&nbsp;</td>
  </tr>
</table>

# 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 () {
  
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console