Plan a Program Before You Code It

You can now repeat code with while and for, end a loop early with break, and skip to its next pass with continue. Soon you’ll combine them in the secret-code guessing game. However, a whole game is a lot to face in an empty file.

So before typing code, you’ll turn a task into a plan you can check. In this lesson, you’ll plan one small program with pen and paper, then build it one runnable step at a time, just as you’ll do for the game later.

Restate the Brief as Inputs and an Output

To practice planning, you’ll start with a small code-matching tool, separate from your game. Your game will answer each guess with higher or lower, but this tool uses a different rule:

Tell the player how many digits of their guess are in the right place. The secret code and the guess are both four digits long.

A brief describes the task in everyday words. Before you write any code, restate it in three parts:

  • Inputs: The values the program starts from. Here, the secret code and one guess, both strings of four digits.
  • Output: The result it reports. Here, one number, the count of places where the guess matches the secret.
  • Steps: What happens in between. You’ll find those next.

Naming the output tells you what a finished program must produce, so you’ll recognize it when it works.

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.