pxt-calliope/docs/reference/js/lessons/guess-the-number/activity.md

43 lines
855 B
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# guess the number activity
2016-04-02 01:22:47 +02:00
guess the number with math random.
2016-03-26 00:47:20 +01:00
### ~avatar avatar
### @video td/videos/guess-the-number-0
Welcome! This tutorial will help you create a guess the number game! Let's get started!
### ~
Add an event handler when button `A` is pressed.
```
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
})
```
Create a local variable of type number `x` and set it to a random number using `math->random`. `math->random(10)` generates a random number between `0` and `10` **excluded**.
```
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
let x = Math.random(10)
})
```
Show the random number on the screen.
```
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
let x1 = Math.random(10)
basic.showNumber(x1, 150)
})
```
### ~avatar avatar
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/guess-the-number/challenges)!
2016-03-26 00:47:20 +01:00
### ~