Introduction: Making the Game Interactive

Welcome to the next step in building your Word Play Game! So far, you have created the main HTML structure and styled your game with CSS. Now, it’s time to make your game interactive using JavaScript.

In this lesson, you will learn how JavaScript connects your static HTML and CSS to the game logic. By the end, you will understand how the game responds to user actions, updates the page, and communicates with the backend to keep the game running smoothly.

Quick Recall: Connecting Frontend and Backend

Let’s quickly remind ourselves of what you have already built:

  • Your HTML file sets up the structure of the game, including buttons, input fields, and areas to display information.
  • Your CSS file makes the game look nice and organized.

Now, JavaScript will be the “bridge” that listens for user actions (like button clicks), updates the page, and communicates with the backend server to get or send game data.

Managing Game State and Starting the Game

To make the game work, we need to keep track of what’s happening as the player plays. This is called the “game state.” In JavaScript, we use variables to remember things like which word we’re on, the player’s score, and whether the game has started.

Here’s how the game state is set up:

  • words will hold the list of words the LLM generated.
  • breakpoints tell us where the player should make a guess.
  • currentWord keeps track of which word we’re showing.
  • scoreSum is the player’s total score.
  • gameStarted helps us know if the game has started.
  • guessHistory will store all the guesses the player has made.

To start the game, we need to get the game data from the backend. This is done with the startGame function:

  • fetch("/game") asks the backend for the game data.
  • The response is turned into a JavaScript object with await res.json().
Displaying Words and Handling Guesses

Now, let’s see how the game shows words and waits for the player’s guess.

The showNextWord function is responsible for displaying each word one at a time:

Let’s break this down:

  • If we’ve shown all the words, the game ends and the final score is displayed.
  • If we reach a breakpoint, the game shows the guess input box and waits for the player to enter a guess.
  • Otherwise, the next word is shown on the page, and after a short pause (600 milliseconds), the function is executed again.

When the player makes a guess and clicks the submit button, this code runs:

Here’s what happens:

  • The player’s guess is read from the input box.
  • The correct word is found from the words list.
  • If the guess is empty, nothing happens.
Updating the Score and Showing Results

After each guess, the score is updated and shown to the player. When the game ends, the final score and leaderboard are displayed.

Here’s how the score table is updated:

  • This function adds a new row to the guess table with the player’s guess, the correct word, and the score for that guess.
  • The table is made visible if it was hidden.

When the game is over, the player can submit their score to the leaderboard:

  • The player’s name and score are sent to the backend.
  • The leaderboard is updated and shown on the page.
Updating the Page with JavaScript

Throughout the game, JavaScript is used to show or hide different parts of the page and update the content. Here are some examples:

  • To hide or show a block, we use classList.add("hidden") or classList.remove("hidden").
  • To update text, we use element.textContent = ....
  • To add new elements, we use document.createElement() and appendChild().

For example, when the game starts, we hide the start block and show the game info:

This makes the game feel dynamic and responsive to the player’s actions.

Summary And What’s Next

In this lesson, you learned how JavaScript brings your Word Play Game to life by:

  • Managing the game state with variables.
  • Fetching game data from the backend and starting the game.
  • Displaying words and handling player guesses.
  • Updating the score and showing results.
  • Dynamically updating the page as the game progresses.

You are now ready to practice these skills in the next set of exercises. You’ll get hands-on experience writing and modifying JavaScript code to control the game’s flow and make your web app interactive. Good luck!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal