? Week 5 Day 5 Challenge — Number Duel
CodeX Academy · Level 1 — App Lab · Lesson 10

Number Duel

Optional extra practice for the same state-and-branch skill as tonight's demo (Dice Duel) — a different scenario, on your own. Same deterministic testing habit applies here too.

1. Build this in Design Mode first

STEP 1

Create these elements on screen1 with these exact IDs and starting text.

TypeIDText
LabelplayerLabelYou: --
LabelcomputerLabelComputer: --
ButtonspinBtnSpin
LabelresultLabelResult: --

2. Switch to Code Mode

STEP 2

Copy this starter code into Code Mode and fill in the marked STEP. Same pattern as tonight's demo.

onEvent("spinBtn", "click", function() {
  var playerNum = randomNumber(1, 10);
  var computerNum = randomNumber(1, 10);
  setText("playerLabel", "You: " + playerNum);
  setText("computerLabel", "Computer: " + computerNum);

  // STEP: if / else if / else with === and >, set resultLabel:
  //   playerNum === computerNum -> "Result: Tie!"
  //   playerNum > computerNum    -> "Result: You win!"
  //   otherwise                  -> "Result: Computer wins!"

});

3. Check yourself — test deterministically first

STEP 3
  • Force var playerNum = 7; and var computerNum = 7;. Confirm Result: Tie!
  • Force var playerNum = 9; and var computerNum = 3;. Confirm Result: You win!
  • Force var playerNum = 2; and var computerNum = 8;. Confirm Result: Computer wins!
  • Restore both randomNumber(1, 10) lines and confirm real spins still work across several clicks.

Try it for real before checking the answer key.

Answer key
onEvent("spinBtn", "click", function() {
  var playerNum = randomNumber(1, 10);
  var computerNum = randomNumber(1, 10);
  setText("playerLabel", "You: " + playerNum);
  setText("computerLabel", "Computer: " + computerNum);

  if (playerNum === computerNum) {
    setText("resultLabel", "Result: Tie!");
  } else if (playerNum > computerNum) {
    setText("resultLabel", "Result: You win!");
  } else {
    setText("resultLabel", "Result: Computer wins!");
  }
});