Project: A Secret-Code Guessing Game

At the very start of this course, you met a guessing game where the player hunts for a secret code. The checkGuess() function you warmed up with could judge one guess, but each guess needed its own handwritten call. Since then, you’ve learned to repeat code with loops, end them early with break or skip ahead with continue, plan a program on paper, and ask a real player questions in the terminal.

In this project, you’ll put those pieces together. You’ll plan the secret code guessing game, then build it as a local script in small steps, checking each one before you add the next.

Read the Brief

Here’s what your finished game should do:

The player tries to find a secret code, a whole number that you pick. The game asks for one guess at a time. A blank answer, an answer that isn’t a number, or a number outside the allowed range gets a short explanation and a fresh question. A usable guess that misses the code gets a hint to go higher or lower. The game ends when the player finds the code or types quit, and then it hands the terminal back.

There are no random numbers here. You choose the secret code yourself and write it into the script. That means anyone who reads your file knows the answer, so hand the keyboard to a friend or a family member, or play against your future self after you’ve forgotten the number.

Plan the Game on Paper

Before you open the editor, take out a pen and paper and restate the brief in your own words, as you did in the lesson on planning:

  • Inputs: What values does the game need, and where does each one come from?
  • Output: What should the player see, and how does the game finish?
  • Steps: What happens to each answer, in order? Write one short line per step and mark which ones repeat.

Then test your plan by hand. Say the secret code is 37 and guesses can go from 1 to 100. For each answer below, write down what the game should say and whether it asks again:

  • 50
  • An empty answer, where the player only presses Enter
  • abc
  • 150
  • 25
  • 37
  • Quit, with a capital Q

You should end up with “go lower” for 50 and “go higher” for 25. The empty answer, abc, and 150 all get an explanation and another question. The answer 37 wins. A person would read Quit as quitting, so your game should, too, even with a capital letter or a stray space around it. If a turn surprised you, fix your plan.

Only two answers end the game. Every other answer leads back to the question, so the game is a while (true) loop with two ways out. Now compare your plan with this sample:

  • Inputs: The secret code and the allowed range, which you write into the script, and the player’s answers, which arrive one at a time as strings
  • Output: A message for every answer and a clean finish when the player wins or quits
  • Steps:
Language: Text
The secret code and the range of allowed guesses
Repeat:
  Ask for a guess
  If the player wants to quit, say goodbye and stop
  If the guess isn't usable, explain and ask again
  Compare the guess with the secret code
  If it matches, celebrate and stop
  Otherwise, say higher or lower and ask again
Close the interface

Your wording can differ. Check that yours asks inside the repeat, checks for a quit before it validates the guess, and closes the interface once, after the repeat.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.