You’ve come a long way since the first lesson. Back then, your checkGuess() function could judge a single guess, and every guess needed its own handwritten call. Now you have a game that asks a real player for as many guesses as they need, explains odd answers, gives hints, and hands the terminal back when the player wins or types quit.
Before you read the list below, take a minute to pull a few ideas from memory. With pen and paper, write down the four parts of a while loop, where break and continue each jump to, and the two ways your game can end. Then compare your notes with the rest of this page.
What You Can Do Now
In this course, you’ve learned how to:
- Repeat code with
while: You give awhileloop a start, a condition, a body, and an update that moves the condition towardfalse. You trace its passes in a table, predict that a loop whose first check fails runs zero times, and stop a runaway loop with Ctrl+C. When the body must run once before there’s anything to check, you reach fordo...while. - Count with
for: Aforheader packs the start, the check, and the update into one line and runs them in a fixed order: the start once, then check, body, and update, until the check isfalse. You declare an accumulator before the loop and use its result after it, and you visit a string’s characters withi < word.lengthwithout reading past the end. - Leave a loop early:
breakends the whole loop, for example at the first match, andcontinueskips to the next pass. In a countedwhileloop, keep the update above anycontinueso every pass reaches it. Your game’swhile (true)loop is different: its condition never becomesfalse, so it ends only when a quit or a win reaches abreak. - Plan before you code: You restate a brief as inputs and an output, solve two or three cases by hand, write the steps as comments, and build under them one runnable step at a time.
- Ask questions in the terminal: With the readline recipe in an
.mjsfile, you ask withawaitrl.question(), and every answer arrives as a string. You convert it withNumber(), reject blank,NaN, and out-of-range answers, ask again, and close the interface once, after the loop.