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 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.

Quick-add: + add another resource

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.

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
Loading ..................

Console