Code Studio · Level 10
Rules Builder
Students add more rules for score, locked doors, and required items.
Build a locked-door rule.
Example code
class Player {
constructor(name) {
this.name = name;
this.score = 0;
this.inventory = [];
}
collect(item, points) {
this.inventory.push(item);
this.score += points;
}
canOpenDoor() {
return this.inventory.includes("key") && this.score >= 10;
}
}
let explorer = new Player("Nova");
explorer.collect("star", 10);
explorer.collect("key", 5);
if (explorer.canOpenDoor()) {
say("Door unlocked!");
} else {
say("Find the key and earn 10 points.");
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
Rules builder complete.
You built a game rule that checks both inventory and score.
Try Rules Challenge