Platform
Resources
Pricing
Sign in
Get started
Feng Yifei
Let's build a better world, together.
Workspace
Fork
Public
By
Feng Yifei
Edited
Oct 18, 2023
1 fork
3 stars
4
Insert cell
Insert cell
Insert cell
Insert cell
{
var
age
=
12
;
// number
var
isString
=
true
;
// boolean
let
name
=
"Zhang San"
;
// string
let
isEven
=
(
n
)
=>
n
%
2
===
0
;
// function
age
=
true
;
// 变量赋值可以修改类型
return
age
,
isEven
(
3
)
;
// 返回最后一个表达式
}
Insert cell
{
const
PI
=
3.1415
;
try
{
PI
=
1
;
// 抛出异常
}
catch
(
e
)
{
return
e
.
message
;
}
}
Insert cell
Insert cell
{
return
[
1
/
0
,
1
/
Infinity
,
1
+
Infinity
,
1
-
Infinity
,
Infinity
-
Infinity
,
(
1
/
Infinity
)
*
Infinity
]
;
}
Insert cell
Insert cell
{
return
[
(
5
/
3
)
|
0
,
Math
.
floor
(
5
/
3
)
,
parseInt
(
5
/
3
)
,
(
5
/
3
)
.
toFixed
(
0
)
]
;
}
Insert cell
{
const
quickPower
=
(
base
,
n
)
=>
{
if
(
n
==
0
)
return
1
;
if
(
n
==
1
)
return
base
;
if
(
n
==
2
)
return
base
*
base
;
const
half
=
(
n
/
2
)
|
0
;
return
quickPower
(
base
,
half
)
*
quickPower
(
base
,
n
-
half
)
;
}
;
return
[
[
1
,
0
]
,
[
2
,
2
]
,
[
2
,
3
]
,
[
2
,
200
]
,
[
3
,
5
]
]
.
map
(
(
[
base
,
n
]
)
=>
quickPower
(
base
,
n
)
)
;
}
Insert cell
{
// n! = n * (n - 1) * ... * 1 => f(n) = nf(n - 1)
const
factorial
=
(
n
)
=>
{
if
(
n
==
1
)
return
1
;
return
n
*
factorial
(
n
-
1
)
;
}
;
return
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
]
.
map
(
(
n
)
=>
factorial
(
n
)
)
;
}
Insert cell
Insert cell
{
const
student
=
{
// 大括号
name
:
"fengyfei"
,
// key: value
age
:
21
}
;
return
{
student
// key 为变量名,value 为变量指向的对象
}
;
}
Insert cell
Insert cell
{
const
numbers
=
[
1
,
2
,
3
]
;
return
{
value
:
numbers
,
length
:
numbers
.
length
}
;
}
Insert cell
{
const
numbers
=
[
1
,
2
,
3
,
4
]
;
numbers
[
6
]
=
7
;
let
In
=
""
;
for
(
let
idx
in
numbers
)
{
// in 遍历时返回的索引总数未必等于数组长度
In
+=
idx
+
" "
;
}
let
Of
=
""
;
for
(
let
val
of
numbers
)
{
Of
+=
val
+
" "
;
}
return
{
length
:
numbers
.
length
,
In
,
Of
}
;
}
Insert cell
{
const
numbers
=
[
1
,
2
,
3
,
4
]
;
numbers
.
length
=
2
;
// numbers.length = 7;
let
In
=
""
;
for
(
let
idx
in
numbers
)
{
// in 遍历时返回的索引总数未必等于数组长度
In
+=
idx
+
" "
;
}
return
In
;
}
Insert cell
Insert cell
{
const
expr
=
[
3
,
1.0
,
NaN
,
"fengyfei"
,
true
,
false
,
null
,
undefined
,
function
(
)
{
}
]
;
return
expr
.
map
(
(
elm
)
=>
`${
elm
} -> ${
typeof
elm
}`
)
;
}
Insert cell
{
const
expr
=
[
"typeof {}"
,
"typeof []"
]
;
return
expr
.
map
(
(
elm
)
=>
`${
elm
} -> ${
eval
(
elm
)
}`
)
;
}
Insert cell
Insert cell
{
const
expr
=
[
"0 === 0"
,
"0 === 0.0"
,
"0 == '0'"
,
// '0' 转换为整数
"0 === '0'"
,
"NaN === NaN"
,
"NaN == null"
,
"NaN == undefined"
,
"1 / 0 === Infinity"
,
"0 == null"
,
"0 == undefined"
,
"0.1 + 0.1 === 0.2"
,
"0.1 + 0.2 === 0.3"
,
"0.1 + 0.2"
]
;
let
content
=
"|表达式|结果|\n| --- | --- |\n"
;
// markdown 表格头
expr
.
forEach
(
(
elm
)
=>
(
content
+=
`|${
elm
}|${
eval
(
elm
)
}|\n`
)
)
;
// 表格内容
return
md
`${
content
}`
;
}
Insert cell
{
return
[
{
}
==
{
}
,
[
]
==
[
]
,
{
}
==
[
]
,
0
==
{
}
]
;
}
Insert cell
Insert cell
Insert cell
{
const
value
=
10
;
if
(
value
%
2
===
0
)
{
return
"Even"
;
}
else
{
return
"Odd"
;
}
}
Insert cell
{
const
number
=
10
;
const
check
=
(
number
)
=>
{
if
(
number
>
0
)
{
return
"Positive"
;
}
else
if
(
number
===
0
)
{
return
"Zero"
;
}
else
{
return
"Negative"
;
}
}
;
return
[
-
Infinity
,
-
1
,
-
0
,
0
,
1
,
Infinity
,
NaN
,
null
,
undefined
]
.
map
(
(
n
)
=>
check
(
n
)
)
;
}
Insert cell
{
const
level
=
(
n
)
=>
{
if
(
n
>=
95
)
{
return
"A+"
;
}
else
if
(
n
>=
85
)
{
return
"A"
;
}
else
if
(
n
>=
75
)
{
return
"B"
;
}
else
if
(
n
>=
60
)
{
return
"C"
;
}
else
{
return
"D"
;
}
}
;
return
[
96
,
95
,
94
,
86
,
85
,
84
,
76
,
75
,
74
,
61
,
60
,
59
]
.
map
(
(
n
)
=>
level
(
n
)
)
;
}
Insert cell
Insert cell
{
let
sum
=
0
;
for
(
let
n
=
1
;
n
<
11
;
n
++
)
{
sum
+=
n
;
}
return
sum
;
}
Insert cell
{
let
matrix
=
"\n"
;
const
max
=
10
;
for
(
let
i
=
1
;
i
<
max
;
i
++
)
{
for
(
let
k
=
1
;
k
<=
i
;
k
++
)
{
if
(
i
*
k
<
max
)
{
matrix
+=
" "
;
}
matrix
+=
i
*
k
+
" "
;
}
matrix
+=
"\n"
;
}
return
matrix
;
}
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML