Migrate docs from the other repo
This commit is contained in:
114
docs/reference/js/lessons/magic-8/activity.md
Normal file
114
docs/reference/js/lessons/magic-8/activity.md
Normal file
@ -0,0 +1,114 @@
|
||||
# magic 8 activity
|
||||
|
||||
a fortune teller game on the BBC micro:bit #docs
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
Welcome! This tutorial will help you create a magic 8 ball on the BBC micro:bit. Let's get started!
|
||||
|
||||
### ~
|
||||
|
||||
To create a new script, go to the [Create Code](/microbit/create-code) page and tap *New Project* under **Touch Develop**.
|
||||
|
||||
Show a string to instruct the user how to play Magic 8! The magic 8 ball can only answer true or false questions.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
```
|
||||
|
||||
Display the number 8.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
```
|
||||
|
||||
* Run your program
|
||||
|
||||
Register code to run when the BBC micro:bit is shaken.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
})
|
||||
```
|
||||
|
||||
Create a variable of type number called **x**.
|
||||
|
||||
Set **x** equal to a random number with a limit of 3.
|
||||
|
||||
Remember the random function in the math library, picks a random number from 0 to the limit, but not including the limit unless it is 0.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x = Math.random(3)
|
||||
})
|
||||
```
|
||||
|
||||
Create an if statement for the condition `if x= 2`.
|
||||
|
||||
If **x** is 2, display the string 'Yes'
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x1 = Math.random(3)
|
||||
if (x1 == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Create an if statement for the condition `if x = 1`.
|
||||
|
||||
If ``x`` is 1, display the string 'No'
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x2 = Math.random(3)
|
||||
if (x2 == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (x2 == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
If **x** is not 2 or 1, it must be 0. This is the else condition.
|
||||
|
||||
If **x** is 0, display the string 'I don't know'
|
||||
|
||||
Display the number 8 so users know they can ask the magic 8 ball another question!
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
basic.clearScreen()
|
||||
let randomNumber = Math.random(3)
|
||||
if (randomNumber == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (randomNumber == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
basic.showNumber(8, 150)
|
||||
})
|
||||
```
|
||||
|
||||
* Run your program
|
||||
|
||||
### ~avatar avatar
|
||||
|
||||
Excellent, you're ready to continue with the [challenges](/microbit/lessons/magic-8/challenges)!
|
||||
|
||||
### ~
|
||||
|
105
docs/reference/js/lessons/magic-8/challenges.md
Normal file
105
docs/reference/js/lessons/magic-8/challenges.md
Normal file
@ -0,0 +1,105 @@
|
||||
# magic 8 challenges
|
||||
|
||||
Coding challenges for the magic 8 tutorial #docs
|
||||
|
||||
## Before we get started
|
||||
|
||||
Complete the following guided tutorial:
|
||||
|
||||
* [tutorial](/microbit/lessons/magic-8-ball/tutorial)
|
||||
|
||||
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x = Math.random(3)
|
||||
if (x == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (x == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
basic.showNumber(8, 150)
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Now let's increase the number of responses the magic 8 ball can give. How about 5 responses instead? Let's change the limit of `math->random` to 5.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x1 = Math.random(5) // ***
|
||||
if (x1 == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (x1 == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
basic.showNumber(8, 150)
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 2**
|
||||
|
||||
Now have the magic 8 ball respond ``"Try again"`` if ``x`` is 3.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x2 = Math.random(5)
|
||||
if (x2 == 3) {
|
||||
basic.showString("Try again", 150) // ***
|
||||
} else if (x2 == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
}
|
||||
else if (x2 == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
basic.showNumber(8, 150)
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 3**
|
||||
|
||||
Now what about if ``x`` is 4? Let's have the magic 8 ball respond ``"Definitely!"``.
|
||||
|
||||
```
|
||||
basic.showString("Ask a question", 150)
|
||||
basic.showNumber(8, 150)
|
||||
input.onGesture(Gesture.Shake, () => {
|
||||
let x3 = Math.random(5)
|
||||
if (x3 == 4) {
|
||||
basic.showString("Definitely!", 150) // ***
|
||||
} else if (x3 == 3) {
|
||||
basic.showString("Try again", 150)
|
||||
}
|
||||
else if (x3 == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
}
|
||||
else if (x3 == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
basic.showNumber(8, 150)
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 4**
|
||||
|
||||
Add 3 more responses so your magic 8 ball has 8 possible responses. Be creative!
|
||||
|
57
docs/reference/js/lessons/magic-8/quiz-answers.md
Normal file
57
docs/reference/js/lessons/magic-8/quiz-answers.md
Normal file
@ -0,0 +1,57 @@
|
||||
# magic 8 quiz answers
|
||||
|
||||
create a magic 8 ball on the BBC micro:bit #math #random #docs
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [magic 8 tutorial](/microbit/lessons/magic-8/tutorial).
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. Define what an 'if statement' is.
|
||||
|
||||
<br />
|
||||
|
||||
An if statement will conditionally run code depending on whether or not a condition is true.
|
||||
|
||||
## 2. Create a Variable called ``x`` and assign it to a random number between 0 and 2.
|
||||
|
||||
```
|
||||
let x = Math.random(3)
|
||||
```
|
||||
|
||||
## 3. Write the 'if statement' to check if ``x`` is equal to 2. Inside the 'if statement', display the string "Yes".
|
||||
|
||||
```
|
||||
if (x == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Write the 'if statement' to check if ``x`` is equal to 1. Inside the 'if statement', display the string "No."
|
||||
|
||||
```
|
||||
if (x == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (x == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Write the code to display the string "I don't know" if the Variable ``x`` is neither 2 nor 1.
|
||||
|
||||
```
|
||||
if (x == 2) {
|
||||
basic.showString("Yes", 150)
|
||||
} else if (x == 1) {
|
||||
basic.showString("No", 150)
|
||||
}
|
||||
else {
|
||||
basic.showString("I don't know", 150)
|
||||
}
|
||||
```
|
||||
|
||||
Note: Students are only required to write the bottom half of this answer (starting with "else").
|
||||
|
32
docs/reference/js/lessons/magic-8/quiz.md
Normal file
32
docs/reference/js/lessons/magic-8/quiz.md
Normal file
@ -0,0 +1,32 @@
|
||||
# magic 8 quiz
|
||||
|
||||
create a magic 8 ball on the BBC micro:bit #math #random #docs
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [magic 8 tutorial](/microbit/lessons/magic-8/tutorial).
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. Define what an 'if statement' is.
|
||||
|
||||
<br />
|
||||
|
||||
## 2. Create a Variable called ``x`` and assign it to a random number between 0 and 2.
|
||||
|
||||
<br />
|
||||
|
||||
## 3. Write the 'if statement' to check if ``x`` is equal to 2. Inside the 'if statement', display the string "Yes".
|
||||
|
||||
<br />
|
||||
|
||||
## 3. Write the 'if statement' to check if ``x`` is equal to 1. Inside the 'if statement', display the string "No."
|
||||
|
||||
<br />
|
||||
|
||||
## 5. Write the code to display the string "I don't know" if the Variable ``x`` is neither 2 nor 1.
|
||||
|
||||
<br />
|
||||
|
Reference in New Issue
Block a user