pxt-calliope/olddocs/js/lessons/transformers/quiz-answers.md

66 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2016-03-26 00:47:20 +01:00
# transformers quiz answers
2016-04-02 01:22:47 +02:00
Use functions to return values.
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
This is the answer key for the [transformers quiz](/lessons/transformers/quiz).
2016-03-26 00:47:20 +01:00
## 1. What is a 'function'?
A function is a unit of code that performs a specific task and returns a result.
## 2. Write the line of code to create a number variable called "x" is equal to 5.
<br/>
```
let x = 5
```
## 3. Write the line of code to create a condition for 'on button pressed ("A")'
<br/>
```
input.onButtonPressed(Button.A, () => {
2016-03-26 00:47:20 +01:00
})
```
## 4. Write the steps to create a function.
<br/>
Click on `script`, then `add new`, and select `function`.
## 5. Create a function called double that will double whatever input parameter is passed into it.
<br/>
```
export function double(n: number) : number {
let r: number
return n * 2
return r
}
```
## 6. Consider the following directions
Call the `function` that doubles the variable **x**. (The `function` is going to return the doubled value after it is called). Assign the new value (10) to a variable which we will call `doubled`.
<br/>
```
let doubled = double(x)
```
## 7. Refer to Question 6
Write the code to call the function that doubles our new `variable` doubled. Assign the new value 20 to a variable we will call doubled twice.
<br/>
```
let doubleTwice = double(doubled)
```