pxt-calliope/docs/lessons/counter/quiz-answers.md

55 lines
1.4 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# counter quiz answers
Learn how to create a counter with the @boardname@ button.
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
This is the answer key for the [counter quiz](/lessons/counter/quiz).
2016-03-26 00:47:20 +01:00
## 1. What is a variable?
Answers may vary but a variable is a place where you can store and retrieve data
## 2. Draw the stored value for the variable called count
2016-03-30 01:17:34 +02:00
```blocks
2016-03-26 00:47:20 +01:00
let count = 0
```
![](/static/mb/lessons/counter-0.png)
We create a **variable**, `count` to keep track of the current count. The number 0 is stored into memory of the variable.
<br/>
## 3. Draw which LEDs are ON after running this code and pressing button "A" once. Explain you chose to draw that number
2016-03-30 01:17:34 +02:00
```blocks
2016-03-30 23:19:51 +02:00
let count = 0
input.onButtonPressed(Button.A, () => {
2016-03-30 23:19:51 +02:00
count = count + 1
basic.showNumber(count)
2016-03-26 00:47:20 +01:00
})
```
![](/static/mb/lessons/counter-1.png)
2016-11-02 01:44:37 +01:00
We are only pressing on button pressed once. So the number to display on the @boardname@ is also one.
2016-03-26 00:47:20 +01:00
<br/>
## 4. Draw which LEDs are ON after running this code and pressing button "A" three times. Explain you chose to draw that number
2016-03-30 01:17:34 +02:00
```blocks
2016-03-30 23:19:51 +02:00
let count = 0
input.onButtonPressed(Button.A, () => {
2016-03-30 23:19:51 +02:00
count = + 1
basic.showNumber(count)
2016-03-26 00:47:20 +01:00
})
```
![](/static/mb/lessons/counter-2.png)
We included the code ``on button pressed("A")`` that runs each time the user presses A. The code increments `count` by `1`. We increase `count` by 1 whenever the user presses the button. So the third time the A button is pressed on the @boardname@, the number 3 is displayed
2016-03-26 00:47:20 +01:00
<br/>