-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjavascript.cbmd
More file actions
81 lines (50 loc) · 1.63 KB
/
javascript.cbmd
File metadata and controls
81 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
title: "Codebraid with JavaScript"
---
## Inline code
### Run
Inline code with `.cb-run` gives raw stdout. While the language can be
specified with `.javascript`, `.js` works as well.
`console.log(1 + 2);`{.js .cb-run example=true}
### Expression and inline notebook
Inline code with `.cb-expr` evaluates an expression and then inserts the raw
output into the document, where it is interpreted as Markdown. Inline code
with `.cb-nb` (`nb` is short for `notebook`) is similar, except output is
shown verbatim.
`` `\$2^8 = ${2**8}\$` ``{.js .cb-expr example=true}
`4*16`{.js .cb-nb example=true}
### Stderr
In the event of an error, inline code automatically shows stderr by default.
This code is executed in its own session, `inline_error`, so that it does
not impact other examples.
`console.logs(1 + 2);`{.js .cb-run session=inline_error example=true}
## Block code
### Run
Code blocks with `.cb-run` give raw stdout. There is continuity between code
blocks so long as they are in the same session; variables persist.
```{.js .cb-run example=true}
message = "Hello from *JavaScript!*";
```
```{.js .cb-run example=true}
console.log(message);
```
### Notebook
Code blocks with `.cb-nb` show the code and also the verbatim stdout.
```{.js .cb-nb session=notebook example=true}
function pows_of_two(start, end) {
n = 2;
x = start;
while (x < end) {
process.stdout.write(String(2**x) + ", ");
x += 1;
}
console.log(2**end);
}
pows_of_two(1, 9);
pows_of_two(1, 15);
```
### Stderr
Code blocks show stderr automatically by default.
```{.js .cb-nb session=block_error example=true}
x = 1 + ;
```