thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

16.07 · Input & output / Learn · 5 min

Ask again after a wrong answer

Combine async input, a loop and an update in a small terminal repair.

Activity 7 of 7 in Input & output

All courses

JavaScript Basics / 16.07

Ask again after a wrong answer

Not started

Combine async input, a loop and an update in a small terminal repair.

More options
Rob sees a failed reply, enters a new answer, then finishes and closes the terminal program after success.
Rob’s comic strip · Input & output

Your terminal task

Save the starter as passphrase.cjs. Run node passphrase.cjs. The program incorrectly prints Success even after a wrong first reply.

Replace the single check with a loop. After printing Wrong passphrase, ask Demo passphrase: again and store the reply in attempt. Print Success only after the reply equals code-zone.

Your terminal task
passphrase.cjsjavascript · example
const readline = require("node:readline/promises");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

async function start() {
  try {
    const expected = "code-zone";
    let attempt = await rl.question("Demo passphrase: ");
    // Repeat this check and ask for a new attempt when it is wrong.
    if (attempt !== expected) {
      console.log("Wrong passphrase");
    }
    console.log("Success");
  } finally {
    rl.close();
  }
}

start().catch((error) => {
  console.error(error.message);
  process.exitCode = 1;
});

Test a conversation

Try code-zone first: expect Success with no failure message. Restart and enter wrong, then code-zone: expect one Wrong passphrase, another question, then Success.

Restart again and try a blank answer, then Code-zone, then code-zone: the first two replies should fail because the rule is an exact match. Each new answer must be read after its question appears. Use Control+C if you want to stop.

One possible terminal solution

Reveal the example and explanation

The loop checks attempt !== expected. The assignment inside the body replaces the wrong value with a fresh reply, so the next check can become false.

This local terminal task has no browser Check challenge button. Its companion comparison practice supports saved drafts, instructor submissions and community code sharing. You can copy this terminal code into a community question, but do not claim browser checks verified the Node.js conversation.

One possible terminal solution
passphrase.cjsjavascript · example
const readline = require("node:readline/promises");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

async function start() {
  try {
    const expected = "code-zone";
    let attempt = await rl.question("Demo passphrase: ");
    while (attempt !== expected) {
      console.log("Wrong passphrase");
      attempt = await rl.question("Demo passphrase: ");
    }
    console.log("Success");
  } finally {
    rl.close();
  }
}

start().catch((error) => {
  console.error(error.message);
  process.exitCode = 1;
});

Compare a passphrase exactly

Work through 5 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

5 steps · Saved work · Instant feedback

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

A retry loop must read a fresh answer each time it repeats.