2016-03-26 00:47:20 +01:00
|
|
|
# counter challenges
|
|
|
|
|
|
|
|
Coding challenges for the counter tutorial.
|
|
|
|
|
|
|
|
## Before we get started
|
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
Complete the following [guided tutorial](/lessons/counter/activity) At the end of the tutorial, your code should look like this:
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
let count = 0
|
2016-03-30 06:17:57 +02:00
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-03-26 00:47:20 +01:00
|
|
|
count = count + 1
|
|
|
|
basic.showNumber(count, 150)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Challenge 1
|
|
|
|
|
|
|
|
### @video td/videos/counter-1-2
|
|
|
|
|
|
|
|
Let's add the code to `count` when `B` is pressed. Add an event handler with `input->on button pressed(B)` then add the code to `count`.
|
|
|
|
|
|
|
|
```
|
|
|
|
let count1 = 0
|
2016-03-30 06:17:57 +02:00
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-03-26 00:47:20 +01:00
|
|
|
count1 = count1 + 1
|
|
|
|
basic.showNumber(count1, 150)
|
|
|
|
})
|
2016-03-30 06:17:57 +02:00
|
|
|
input.onButtonPressed(Button.B, () => {
|
2016-03-26 00:47:20 +01:00
|
|
|
count1 = count1 - 1 // ***
|
|
|
|
basic.showNumber(count1, 150) // ***
|
|
|
|
}) // ***
|
|
|
|
```
|
|
|
|
|
|
|
|
### Challenge 3
|
|
|
|
|
|
|
|
Now let's try to reset the counter when the BBC micro:bit is shaken. You will need to register an event handler with `input->on shake`.
|
|
|
|
|