Migrate docs from the other repo
This commit is contained in:
104
docs/reference/js/lessons/jailbreak/challenges.md
Normal file
104
docs/reference/js/lessons/jailbreak/challenges.md
Normal file
@ -0,0 +1,104 @@
|
||||
# jailbreak challenges
|
||||
|
||||
Coding challenges for the jailbreak tutorial.#docs
|
||||
|
||||
## Before we get started
|
||||
|
||||
Complete the following guided tutorial:
|
||||
|
||||
* [tutorial](/microbit/lessons/jailbreak/tutorial)
|
||||
|
||||
At the end of the tutorial, click `keep editing`. Your code should look like this:
|
||||
|
||||
```
|
||||
count = 0
|
||||
shouldBreak = false
|
||||
input.onButtonPressed("A", () => {
|
||||
shouldBreak = true
|
||||
})
|
||||
while (true) {
|
||||
if (shouldBreak) {
|
||||
basic.showString("I'M OUT!", 150)
|
||||
basic.plotImage(`
|
||||
. # . # .
|
||||
. # . # .
|
||||
. . . . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`)
|
||||
break
|
||||
}
|
||||
count = count + 1
|
||||
basic.showNumber(count, 150)
|
||||
basic.pause(1000)
|
||||
}
|
||||
```
|
||||
|
||||
**Challenge 1**
|
||||
|
||||
Try to remove the `break` in the `if` loop. What problem does this create?
|
||||
|
||||
**Challenge 2**
|
||||
|
||||
Now let's resume the timer again once button `B` is pressed! To do so, begin by creating a condition to know when button `B` is pressed.
|
||||
|
||||
```
|
||||
// **. . .**
|
||||
while (true) {
|
||||
if (shouldBreak) {
|
||||
basic.showString("I'M OUT!", 150)
|
||||
basic.plotImage(`
|
||||
. # . # .
|
||||
. # . # .
|
||||
. . . . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`)
|
||||
break
|
||||
}
|
||||
count = count + 1
|
||||
basic.showNumber(count, 150)
|
||||
basic.pause(1000)
|
||||
}
|
||||
input.onButtonPressed("B", () => {
|
||||
}) // ***
|
||||
```
|
||||
|
||||
Next, set `should break` back to false to indicate we want to run the `while` loop again.
|
||||
|
||||
```
|
||||
// **. . .**
|
||||
input.onButtonPressed("B", () => {
|
||||
shouldBreak = false // ***
|
||||
})
|
||||
```
|
||||
|
||||
And now copy the code from the previous while loop into the condition of `input->on button pressed("B")`. This will resume the counter.
|
||||
|
||||
```
|
||||
// **. . .**
|
||||
input.onButtonPressed("B", () => {
|
||||
shouldBreak = false
|
||||
while (true) {
|
||||
if (shouldBreak) {
|
||||
basic.showString("I'M OUT!", 150) // ***
|
||||
basic.plotImage(`
|
||||
. # . # .
|
||||
. # . # .
|
||||
. . . . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`) // ***
|
||||
break // ***
|
||||
}
|
||||
count = count + 1 // ***
|
||||
basic.showNumber(count, 150) // ***
|
||||
basic.pause(1000) // ***
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Challenge 3**
|
||||
|
||||
Notice that the two `while` loops are identical. Clean up this redundancy in your code by creating another method and then placing the `while` loop in the method.
|
||||
|
46
docs/reference/js/lessons/jailbreak/quiz-answers.md
Normal file
46
docs/reference/js/lessons/jailbreak/quiz-answers.md
Normal file
@ -0,0 +1,46 @@
|
||||
# jailbreak quiz answers
|
||||
|
||||
break out of a counting loop by pressing button "A" #break #string #variables #docs
|
||||
|
||||
This is the answer key for the [jailbreak quiz](/microbit/lessons/jailbreak/quiz).
|
||||
|
||||
## 1. What does a 'break' statement do to a 'loop' ?
|
||||
|
||||
Exit a while or for loop before the loop is complete.
|
||||
|
||||
## 2. Consider the following directions
|
||||
|
||||
Write the line of code that will initialize a number `variable` to 0. Then create a second `variable` that tells us when we should `break` out of the loop. Set the `break` to false.
|
||||
|
||||
```
|
||||
count = 0
|
||||
shouldBreak = false
|
||||
```
|
||||
|
||||
## 3. Consider the following directions
|
||||
|
||||
Write the line of code to stop incrementing `count` when the button is pressed. (Hint: This will set `should break` to true).
|
||||
|
||||
```
|
||||
input.onButtonPressed("A", () => {
|
||||
shouldBreak = true
|
||||
})
|
||||
```
|
||||
|
||||
## 4. Consider the following directions
|
||||
|
||||
Add an `if` statement to determine whether or not we should break out of the loop. Then include the message "I'm Out!" and a smiley face **image** displayed. This will happen right before you `break` from the `while` loop. **Do not include the break **
|
||||
|
||||
```
|
||||
if (shouldBreak) {
|
||||
basic.showString("I'M OUT!", 150)
|
||||
images.createImage(`
|
||||
. # . # .
|
||||
. # . # .
|
||||
. . . . .
|
||||
# . . . #
|
||||
. # # # .
|
||||
`).showImage(0)
|
||||
}
|
||||
```
|
||||
|
34
docs/reference/js/lessons/jailbreak/quiz.md
Normal file
34
docs/reference/js/lessons/jailbreak/quiz.md
Normal file
@ -0,0 +1,34 @@
|
||||
# jailbreak quiz
|
||||
|
||||
break out of a counting loop by pressing button "A" #break #string #variables #docs
|
||||
|
||||
## Name
|
||||
|
||||
## Directions
|
||||
|
||||
Use this activity document to guide your work in the [jailbreak tutorial](/microbit/lessons/jailbreak/tutorial)
|
||||
|
||||
Answer the questions while completing the tutorial. Pay attention to the dialogues!
|
||||
|
||||
## 1. What does a 'break' statement do to a 'loop' ?
|
||||
|
||||
## 2. Consider the following directions
|
||||
|
||||
Write the line of code that will initialize a number variable to 0. Then create a second variable that tells us when we should `break` out of the loop. Set the `break` to false.
|
||||
|
||||
<br/>
|
||||
|
||||
## 3. Consider the following directions
|
||||
|
||||
Write the line of code to stop incrementing `count` when the button is pressed. (Hint: This will set `should break` to true).
|
||||
|
||||
<br/>
|
||||
|
||||
## 4. Consider the following directions
|
||||
|
||||
Add an `if` statement to determine whether or not we should break out of the loop. Then include the message "I'm Out!" and a smiley face **image** displayed. This will happen right before you `break` from the `while` loop. **Do not include the break **
|
||||
|
||||
<br />
|
||||
|
||||
<br/>
|
||||
|
Reference in New Issue
Block a user