Skip to content

Commit 98512c9

Browse files
committed
code sandbox
1 parent cceb0dd commit 98512c9

File tree

4 files changed

+639
-0
lines changed

4 files changed

+639
-0
lines changed

docs/1-trial-session/08-if-statement/index.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: 条件分岐
33
---
44

5+
import Sandpack from "@site/src/components/Sandpack";
6+
57
## if文
68

79
**if文**は、<Term>JavaScript</Term>の<Term>**制御構造**</Term>で、特定の条件下のみで実行されるプログラムを記述することができます。
@@ -17,6 +19,32 @@ if (age < 18) {
1719

1820
このプログラムは、`未成年者の場合は法定代理人の同意が必要です。`と表示しますが、1行目を`const age = 20;`に変更すると何も表示されなくなります。
1921

22+
<Sandpack
23+
files={{
24+
"/index.html": {
25+
code: `<!DOCTYPE html>
26+
<html>
27+
<head>
28+
<meta charset="UTF-8">
29+
<title>条件分岐</title>
30+
</head>
31+
<body>
32+
<script src="script.js"></script>
33+
</body>
34+
</html>`,
35+
hidden: true,
36+
},
37+
"/script.js": `const age = 16;
38+
if (age < 18) {
39+
document.write("未成年者の場合は法定代理人の同意が必要です。");
40+
}`,
41+
}}
42+
options={{
43+
editorHeight: 250,
44+
activeFile: "/script.js",
45+
}}
46+
/>
47+
2048
2行目の`if (age < 18) {`部分がポイントです。ここに差し掛かると、括弧内の<Term>式</Term>`age < 18`が<Term>評価</Term>され、`true`になります。このため、直後の波括弧内の処理が実行されます。
2149

2250
if文の基本構造は
@@ -55,6 +83,34 @@ if (age >= 18) {
5583

5684
この例では、`age >= 18`の<Term>評価</Term>が`false`となるので、`子供です`が表示されます。
5785

86+
<Sandpack
87+
files={{
88+
"/index.html": {
89+
code: `<!DOCTYPE html>
90+
<html>
91+
<head>
92+
<meta charset="UTF-8">
93+
<title>if-else</title>
94+
</head>
95+
<body>
96+
<script src="script.js"></script>
97+
</body>
98+
</html>`,
99+
hidden: true,
100+
},
101+
"/script.js": `const age = 16;
102+
if (age >= 18) {
103+
document.write("大人です");
104+
} else {
105+
document.write("子供です");
106+
}`,
107+
}}
108+
options={{
109+
editorHeight: 250,
110+
activeFile: "/script.js",
111+
}}
112+
/>
113+
58114
## if~else if~else
59115

60116
if~else if~else構文を使うと、複数の条件を重ねることができます。

0 commit comments

Comments
 (0)