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

Checking your account…

14.06 · Function challenges / Watch · 6 min

Compare two dice with Rob

Combine a random helper with a comparison that names the winning player or a draw.

Activity 6 of 7 in Function challenges

All courses

JavaScript Basics / 14.06

Compare two dice with Rob

Not started

Combine a random helper with a comparison that names the winning player or a draw.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob checks three separate dice matchups on a table: player one has six against two, player two has five against one, and the last pair both show three.
Rob’s comic strip · Function challenges

ORIGINAL WALKTHROUGH · 5:41

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

script.js
script.jsjavascript · example
// Code along: Compare two dice with Rob
// Follow the video and build the program yourself.

Compare two dice and handle a draw

Work through 4 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)

4 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
function winner(player1, player2) {
  if (player1 > player2) return "Player 1 wins!";
  if (player2 > player1) return "Player 2 wins!";
  return "Draw!";
}
console.log(winner(6, 2));
console.log(winner(1, 5));
console.log(winner(3, 3));

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Open the code-along notes

Original dice challenge

Create player1 and player2 roll values using a 1–6 random helper. Print both values and say which player wins, or Draw! if they are equal.

Before adding randomness, write winner(player1, player2) and call it with known pairs. This makes each branch easy to inspect. The next practice deliberately starts with a missing draw branch.

Original dice challenge
example.jsjavascript · example
function winner(player1, player2) {
  if (player1 > player2) return "Player 1 wins!";
  if (player2 > player1) return "Player 2 wins!";
  return "Draw!";
}
console.log(winner(6, 2));
console.log(winner(1, 5));
console.log(winner(3, 3));

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

Test both winners and equal rolls instead of hoping random rolls cover them.