1. Build this in Design Mode first
STEP 1Create these elements on screen1 with these exact IDs and starting text.
| Type | ID | Text |
|---|---|---|
| Label | playerLabel | You: -- |
| Label | computerLabel | Computer: -- |
| Button | spinBtn | Spin |
| Label | resultLabel | Result: -- |
2. Switch to Code Mode
STEP 2Copy 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;andvar computerNum = 7;. ConfirmResult: Tie! - Force
var playerNum = 9;andvar computerNum = 3;. ConfirmResult: You win! - Force
var playerNum = 2;andvar computerNum = 8;. ConfirmResult: 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!");
}
});