more migration to common docs (#370)
* more migration to common docs * refactoring js * removed duplicate docs * migrating more docs * more refactoring * migrated browsers page * updated summary
This commit is contained in:
@ -1,65 +0,0 @@
|
||||
# JavaScript Block
|
||||
|
||||
JavaScript is a very powerful language; so powerful that some concepts in
|
||||
JavaScript can't be shown using blocks. When PXT encounters a piece of code
|
||||
that can't be converted into blocks, it instead creates a grey JavaScript block
|
||||
to preserve it. These blocks get converted right back into the original
|
||||
JavaScript when you switch back to the text editor.
|
||||
|
||||
```blocks
|
||||
for (let index = 0; index <= 5; index++) {
|
||||
basic.showNumber(fibonacci(index))
|
||||
}
|
||||
|
||||
function fibonacci(n: number): number {
|
||||
if (n === 0 || n === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
```
|
||||
|
||||
The code in JavaScript blocks cannot be edited, but the blocks
|
||||
themselves can be moved, copied, pasted, and deleted just like any
|
||||
other block.
|
||||
|
||||
|
||||
## How do I get my JavaScript code to convert without any grey blocks?
|
||||
|
||||
The easiest way to write code that will convert cleanly back to blocks
|
||||
is to write the code using blocks and convert it to JavaScript. Many blocks
|
||||
have a very specifc structure to their JavaScript that must be
|
||||
followed in order to have them convert properly. If you have a
|
||||
piece of your code that does not convert, try looking at the block
|
||||
you were expecting and the JavaScript it produces.
|
||||
|
||||
For example, a for-loop must have this structure to convert to a block:
|
||||
|
||||
```typescript
|
||||
for (let x = 0; x <= 5; x++) {
|
||||
}
|
||||
```
|
||||
|
||||
The following examples can't be represented by the blocks:
|
||||
|
||||
```typescript
|
||||
// Condition must be either < or <= with a variable on the left side
|
||||
for (let x = 0; x > -1; x++) {
|
||||
}
|
||||
|
||||
// The variable in the condition and incrementor must be the declared variable
|
||||
for (let x = 0; x <= 5; y++) {
|
||||
}
|
||||
|
||||
// The declared variable must be initialized to 0
|
||||
for (let x = 2; x <= 5; x++) {
|
||||
}
|
||||
|
||||
// All three parts of the for-loop must be present
|
||||
for (;;) {
|
||||
}
|
||||
```
|
||||
|
||||
Try to match the JavaScript of the desired block as closely as
|
||||
possible.
|
@ -1,9 +0,0 @@
|
||||
# Logic
|
||||
|
||||
```cards
|
||||
if(true) {}
|
||||
true;
|
||||
true && false;
|
||||
!true;
|
||||
1 != 0;
|
||||
```
|
@ -1,77 +1,8 @@
|
||||
# Boolean
|
||||
# @extends
|
||||
|
||||
true or false.
|
||||
## #examples
|
||||
|
||||
A Boolean has one of two possible values: `true`; `false`. Boolean (logical) operators (*and*, *or*, *not*) take Boolean inputs and yields a Boolean value. Comparison operators on other types ([numbers](/reference/types/number), [strings](/reference/types/string) yields a Boolean value.
|
||||
|
||||
The following blocks represent the true and false Boolean values, which can be plugged in anywhere a Boolean value is expected:
|
||||
|
||||
```blocks
|
||||
true;
|
||||
false;
|
||||
```
|
||||
|
||||
The next three blocks represent the three Boolean (logic) operators:
|
||||
|
||||
```blocks
|
||||
true && false;
|
||||
true || false;
|
||||
!true;
|
||||
```
|
||||
|
||||
The next six blocks represent comparison operators that yield a Boolean value. Most comparisons you will do involve [numbers](/reference/types/number):
|
||||
|
||||
```blocks
|
||||
42 == 0;
|
||||
42 != 0;
|
||||
42 < 0;
|
||||
42 > 0;
|
||||
42 <= 0;
|
||||
42 >= 0;
|
||||
```
|
||||
|
||||
Boolean values and operators are often used with an [if](/blocks/logic/if) or [while](/blocks/loops/while) statement to determine which code will execute next. For example:
|
||||
|
||||
### Functions that return a Boolean
|
||||
|
||||
Some functions return a Boolean value, which you can store in a Boolean variable. For example, the following code gets the on/off state of `point (1, 2)` and stores this in the Boolean variable named `on`. Then the code clears the screen if `on` is `true`:
|
||||
|
||||
### Boolean operators
|
||||
|
||||
Boolean operators take Boolean inputs and evaluate to a Boolean output:
|
||||
|
||||
### Conjunction: `A and B`
|
||||
|
||||
`A and B` evaluates to `true` if-and-only-if both A and B are true:
|
||||
|
||||
```blocks
|
||||
false && false == false;
|
||||
false && true == false;
|
||||
true && false == false;
|
||||
true && true == true;
|
||||
```
|
||||
|
||||
### Disjunction: `A or B`
|
||||
|
||||
`A or B` evaluates to `true` if-and-only-if either A is true or B is true:
|
||||
|
||||
```blocks
|
||||
false || false == false;
|
||||
false || true == true;
|
||||
true || false == true;
|
||||
true || true == true;
|
||||
```
|
||||
|
||||
### Negation: `not A`
|
||||
|
||||
`not A` evaluates to the opposite (negation) of A:
|
||||
|
||||
```blocks
|
||||
!false == true;
|
||||
!true == false;
|
||||
```
|
||||
|
||||
### Example
|
||||
### Example: ``AND`` operator
|
||||
|
||||
This example turns on LED `3 , 3`, if LEDs `1 , 1` and `2 , 2` are both on:
|
||||
|
||||
@ -81,7 +12,7 @@ if (led.point(1,1) && led.point(2,2)) {
|
||||
}
|
||||
```
|
||||
|
||||
### Comparisons of numbers and strings
|
||||
### Example: Comparisons of numbers and strings
|
||||
|
||||
When you compare two Numbers, you get a Boolean value, such as the comparison `x < 5` in the code below:
|
||||
|
||||
@ -97,8 +28,3 @@ input.onButtonPressed(Button.A, () => {
|
||||
```
|
||||
|
||||
See the documentation on [Numbers](/reference/types/number) for more information on comparing two Numbers. You can also [compare strings](/reference/types/string-functions) using the `equals` function.
|
||||
|
||||
### See also
|
||||
|
||||
[if](/blocks/logic/if), [while](/blocks/loops/while), [number](/reference/types/number)
|
||||
|
||||
|
@ -1,16 +1,6 @@
|
||||
# If
|
||||
# @extends
|
||||
|
||||
### @parent blocks/language
|
||||
|
||||
|
||||
Conditionally run code depending on whether a [Boolean](/blocks/logic/boolean) condition is true or false.
|
||||
|
||||
```blocks
|
||||
if(true) {
|
||||
}
|
||||
```
|
||||
|
||||
Click on the dark blue gear icon (see above) to add an *else* or *if* to the current block.
|
||||
## #examples
|
||||
|
||||
### Example: adjusting screen brightness
|
||||
|
||||
@ -23,8 +13,3 @@ input.onButtonPressed(Button.A, () => {
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[while loop](/blocks/loops/while), [for](/blocks/loops/for), [boolean](/blocks/logic/boolean)
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
# Loops
|
||||
|
||||
```cards
|
||||
for(let i = 0;i<5;i++) {}
|
||||
while(true) {}
|
||||
basic.forever(() => {});
|
||||
```
|
@ -1,13 +1,6 @@
|
||||
# For
|
||||
# @extends
|
||||
|
||||
### @parent blocks/language
|
||||
|
||||
Run part of the program the number of times you say.
|
||||
|
||||
```block
|
||||
for(let i = 0; i <= 4; ++i) {
|
||||
}
|
||||
```
|
||||
## #examples
|
||||
|
||||
### Example: Count to 4
|
||||
|
||||
@ -20,8 +13,3 @@ input.onButtonPressed(Button.A, () => {
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[repeat](/blocks/loops/repeat), [while](/blocks/loops/while), [if](/blocks/logic/if), [show number](/reference/basic/show-number)
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
# Repeat
|
||||
|
||||
Run part of the program the number of times you say.
|
||||
|
||||
### Block Editor
|
||||
|
||||

|
||||
|
||||
### See also
|
||||
|
||||
[for](/blocks/loops/for), [while](/blocks/loops/while), [if](/blocks/logic/if), [show number](/reference/basic/show-number)
|
||||
|
@ -1,15 +1,6 @@
|
||||
# While
|
||||
# @extends
|
||||
|
||||
Repeat code while a [Boolean](/blocks/logic/boolean) `condition` is true.
|
||||
|
||||
```block
|
||||
while(true) {
|
||||
}
|
||||
```
|
||||
|
||||
The while loop has a *condition* that evaluates to a [Boolean](/blocks/logic/boolean) value. After the `do` keyword, add the code that you want to run while the `condition` is `true`. The while loop concludes with `end while`.
|
||||
|
||||
The condition is tested before any code runs. Which means that if the condition is false, the code inside the loop doesn't execute.
|
||||
## #examples
|
||||
|
||||
### Example: diagonal line
|
||||
|
||||
@ -24,8 +15,3 @@ input.onButtonPressed(Button.A, () => {
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### See also
|
||||
|
||||
[on button pressed](/reference/input/on-button-pressed), [for](/blocks/loops/for), [if](/blocks/logic/if), [forever](/reference/basic/forever)
|
||||
|
||||
|
@ -1,36 +0,0 @@
|
||||
# Math
|
||||
|
||||
### [Numeric](/reference/types/number) values: 0, 1, 2, ...
|
||||
|
||||
```block
|
||||
0;
|
||||
1;
|
||||
2;
|
||||
```
|
||||
|
||||
### Arithmetic binary operation (+, -, *, /)
|
||||
|
||||
```block
|
||||
0+1;
|
||||
0-1;
|
||||
1*2;
|
||||
3/4;
|
||||
```
|
||||
|
||||
### Absolute value
|
||||
|
||||
```block
|
||||
Math.abs(-5);
|
||||
```
|
||||
|
||||
### Minimum/maximum of two values
|
||||
|
||||
```block
|
||||
Math.min(0, 1);
|
||||
```
|
||||
|
||||
### Random value
|
||||
|
||||
```block
|
||||
Math.random(5);
|
||||
```
|
@ -1,11 +1,6 @@
|
||||
# On Start
|
||||
# @extends
|
||||
|
||||
An event that runs when the program starts.
|
||||
|
||||
The ``on start`` is a special event that runs when the program starts, before any other event.
|
||||
Use this event to initialize your program.
|
||||
|
||||
## Example
|
||||
## #exstart
|
||||
|
||||
In this example, ``on start`` sets a dimmer brightness on the screen and the button handler shows a string.
|
||||
|
||||
@ -15,14 +10,3 @@ input.onButtonPressed(Button.A, () => {
|
||||
})
|
||||
led.setBrightness(50)
|
||||
```
|
||||
|
||||
## What about JavaScript?
|
||||
|
||||
``on-start`` only exists in the block editor. In JavaScript, all code executes sequentially from the first line.
|
||||
|
||||
## Hey, my events moved!
|
||||
|
||||
When we transform the blocks into JavaScript, we always place all the event registrations (buttons, shake, ...)
|
||||
before launching the ``on start`` code.
|
||||
|
||||
If a block from ``on start`` pauses, other registered events will have the opportunity to run as well.
|
@ -1,21 +0,0 @@
|
||||
# Variables
|
||||
|
||||
[Assign](/blocks/variables/assign) (set) a variable's value
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
```
|
||||
|
||||
Get a variable's value
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
x;
|
||||
```
|
||||
|
||||
[Change](/blocks/variables/change-var) a variable's value
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
x+=1;
|
||||
```
|
@ -1,40 +0,0 @@
|
||||
# Change Value
|
||||
|
||||
Set the value for local and global variables.
|
||||
|
||||
### @parent blocks/change-value
|
||||
|
||||
Change the value of a variable
|
||||
|
||||
```blocks
|
||||
let x = 0
|
||||
x += 1
|
||||
```
|
||||
|
||||
### Declare a variable
|
||||
|
||||
Use the assignment operator to set the value of a [variable](/blocks/variables/var). Change the value of a variable from 0 to 1 using the change item block. Like this:
|
||||
|
||||
```blocks
|
||||
let x = 0
|
||||
x += 1
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
Use the assignment operator to set the value of a [variable](/blocks/variables/var). Change the value of a variable from 0 to 1 using the change item block. Then display the new value of the variable on the LED screen. Like this:
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
x += 1;
|
||||
basic.showNumber(x);
|
||||
```
|
||||
|
||||
### Notes
|
||||
|
||||
* You can use the assignment operator with variables of each of the supported [types](/reference/types).
|
||||
|
||||
### See also
|
||||
|
||||
[variable](/blocks/variables/var), [types](/reference/types)
|
||||
|
13
docs/blocks/variables/change.md
Normal file
13
docs/blocks/variables/change.md
Normal file
@ -0,0 +1,13 @@
|
||||
# @extends
|
||||
|
||||
## #examples
|
||||
|
||||
### Example: show the value of a variable
|
||||
|
||||
Use the assignment operator to set the value of a [variable](/blocks/variables/var). Change the value of a variable from 0 to 1 using the change item block. Then display the new value of the variable on the LED screen. Like this:
|
||||
|
||||
```blocks
|
||||
let x = 0;
|
||||
x += 1;
|
||||
basic.showNumber(x);
|
||||
```
|
Reference in New Issue
Block a user