Skip to content

Commit

Permalink
Randomize operands and operation when pressing "r"
Browse files Browse the repository at this point in the history
  • Loading branch information
Daghall committed Nov 11, 2018
1 parent ab1049f commit ddbe713
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Demo: https://daghall.github.io/arithmetic/
| `*` | Set *operation* to `×`|
| `/` | Set *operation* to `÷`|
| `:` | Toggle visualization mode|
| `r` | Randomize operands and operation|

`0-9` followed by `+`, ``, `×` or `÷` automatically switches active operand,
and switches back if followed by `0-9` again.
8 changes: 8 additions & 0 deletions arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export default class Arithmetic {
this.visualize = !this.visualize;
this.draw();
}

randomize() {
this.operands.left = randomInt(1, 9);
this.operands.right = randomInt(1, 9);
this.operation = this.operations[randomInt(3)];

this.draw();
}
}

function randomInt(from, to) {
Expand Down
3 changes: 3 additions & 0 deletions controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export default function Controls(arithmetic, canvas, properties) {
case "b":
registerKey("b");
break;
case "r":
arithmetic.randomize();
break;
case ":":
arithmetic.toggleVisualization();
break;
Expand Down

0 comments on commit ddbe713

Please sign in to comment.