-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson19.html
292 lines (208 loc) · 10.2 KB
/
lesson19.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!DOCTYPE html>
<html>
<head>
<title>Beginner JavaScript</title>
</head>
<body>
<script src="allPages.js"></script>
<!-- HTML Mixed CodeMirror-->
<script src="codemirror-5.42.0/lib/codemirror.js"></script>
<link rel="stylesheet" href="codemirror-5.42.0/lib/codemirror.css">
<script src="codemirror-5.42.0/mode/htmlmixed/htmlmixed.js"></script>
<script src="codemirror-5.42.0/mode/xml/xml.js"></script>
<script src="codemirror-5.42.0/mode/javascript/javascript.js"></script>
<script src="codemirror-5.42.0/mode/css/css.js"></script>
<style>
h1 {
text-align:center;
}
html, body {
margin:0;
padding:0;
}
p {
margin:8px;
}
</style>
<p>Arrays are very common to encounter in Javascript, such as when using document.querySelectorAll</p>
<p>For loops are usually used to iterate (travel through) arrays.</p>
<p>Read the code, and follow the directions in the comments.</p>
<script>
window.answershown = false
function showanswer() {
if (window.answershown === true) {
document.querySelector("#example > .CodeMirror").remove()
window.answershown = false
document.getElementById("solutionbutton").innerHTML = "Show Solution"
}
else {
window.answershown = true
document.getElementById("solutionbutton").innerHTML = "Hide Solution"
document.getElementById("exampleeditor").value = window.solutionHTML
var example = CodeMirror.fromTextArea(document.getElementById("exampleeditor"), {
name: "htmlmixed",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
tabSize:2,
readOnly:true,
cursorBlinkRate:-1 //Hides cursor
});
}
}
window.solutionHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <style>\n </style>\n <script>\n var array = [3,5,7,9] //Create an array\n \n var paragraph = document.createElement(\"p\")\n var sum = 0\n \n for (let i=0;i<array.length;i++) {\n \tsum += array[i] //Add the value at the current array index to sum\n }\n \n paragraph.innerHTML = sum\n document.body.appendChild(paragraph)\n \n \n \n for (let i=0;i<array.length;i++) {\n \tvar elem = document.createElement(\"p\")\n elem.innerHTML = array[i]\n document.body.appendChild(elem)\n }\n \n </script>\n </body>\n</html>-->";
window.solutionHTML = window.solutionHTML.slice(4)
window.solutionHTML = window.solutionHTML.slice(0, -3)
</script>
<div style="text-align:center;">
<button style="display:inline-block;margin:auto;padding:6px;background-color:aqua"onclick="showanswer()" id="solutionbutton">Show Solution</button>
<button style="display:inline-block;margin:auto;padding:6px;background-color:lightsalmon;"onclick="window.location = self.previousLesson">Go Back</button>
<button style="display:inline-block;margin:auto;padding:6px;background-color:lightgreen;"onclick="window.location = self.nextLesson">Continue</button>
</div>
<div id="example">
<textarea id="exampleeditor" hidden="true"></textarea>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<script>
//HTML that is in editor at start
//.split("\n").join("\\n").split("\t").join("\\t").split("\"").join("\\\"")
window.initialHTML = "<!--<!DOCTYPE html>\n<html>\n <head>\n </head>\n <body>\n <style>\n </style>\n <script>\n var array = [3,5,7,9] //Create an array\n \n var paragraph = document.createElement(\"p\")\n var sum = 0\n \n for (var i=0;i<array.length;i++) {\n \tsum += array[i] //Add the value at the current array index to sum\n }\n \n paragraph.innerHTML = sum\n document.body.appendChild(paragraph)\n \n \n //Now you try. For each index in the array, create an HTML paragraph\n //element containing the value at that index, and add the paragraph\n //element to the page. (A paragraph element containing the\n //value of array[0], another containing array[1], etc.)\n \n \n \n \n //Javascript for loops are similar to those in many other programming languages.\n //They work like this:\n \n //var i=0 - Initialize counter variable. In this case we set it to 0 so that we start at the\n //first element in the array\n \n //i<paragraphs.length - As long as this condition is true, the loop will continue. In this\n //case, we use i<paragraphs.length so that we go through each element in the array\n \n //i=i+1 - This code runs every time the loop finishes. In this case, increase the value of i by 1\n //so that the next time the loop runs, we will be modifying the next element of the array.\n \n \n </script>\n </body>\n</html>-->";
window.initialHTML = window.initialHTML.slice(4)
window.initialHTML = window.initialHTML.slice(0, -3)
</script>
<!-- All the stuff below is CodeMirror-->
<!-- window.initialHTML is the HTML that they should begin with-->
<div id="editorwrapper">
<textarea id="editor"></textarea>
<button id="run">Run!</button>
<button id="download">Download!</button>
<button id="reset">Reset</button>
</div>
<iframe id="result" src="javascript:document.write('Click run - the HTML page will display here')"></iframe>
<script>
function download(content, name, mimetype) {
var link = document.createElement("a");
document.body.appendChild(link);
content = new Blob([content], {type:mimetype})
link.download = name
link.href = URL.createObjectURL(content);
link.click();
document.body.removeChild(link);
}
document.getElementById("editor").value = localStorage.getItem(window.location.href + "htmlcode") || window.initialHTML
var previoustext;
function save() {
var currentValue = editor.getValue()
if (editor.getValue() !== previoustext) {
previoustext = currentValue
localStorage.setItem(window.location.href + "htmlcode", currentValue)
}
if (currentValue === window.initialHTML) {
document.getElementById("reset").style.opacity = "0.4"
document.getElementById("reset").style.cursor = "not-allowed"
}
else {
document.getElementById("reset").style.opacity = "1"
document.getElementById("reset").style.cursor = "default"
}
}
setInterval(save, 100)
function resetCode() {
editor.setValue(window.initialHTML)
}
document.getElementById("reset").addEventListener("click", resetCode)
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
name: "htmlmixed",
autoCloseTags: true,
styleActiveLine: true,
lineWrapping: false,
lineNumbers:true,
tabSize:2
});
function runcode() {
var htmlcode = editor.getValue()
var iframe = document.getElementById("result")
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlcode);
iframe.contentWindow.document.close();
}
document.getElementById("run").addEventListener("click", runcode)
document.getElementById("download").addEventListener("click", function(){
download(editor.getValue(), "website.html", "text/html")
})
setTimeout(function() {
editor.refresh() //Make sure the editor displays all the content, not just some of it.
},1)
</script>
<style>
#editorwrapper {
left:0;
height:50%;
width:60%;
display:inline-block;
float:left;
border-right:1px solid black;
border-top:1px solid black;
position:fixed;
bottom:0;
padding:0;
}
#result {
right:0;
height:50%;
width:40%;
border:0;
border-top:1px solid black;
border-left:1px solid black;
display:inline-block;
float:right;
position:fixed;
bottom:0;
padding:0;
background-color:white;
z-index:4;
}
/*Make the bottom editor display over the solution*/
#editorwrapper {
background-color:white;
opacity: 1;
z-index: 4;
}
.CodeMirror {
width:100%;
height:93%;
}
#run {
width:45%;
height:7%;
font-size:16px;
background-color:#99e0ff;
border:0;
display:inline-block;
float:left;
}
#download {
width:30%;
height:7%;
font-size:16px;
background-color:#99ffe0;
border:0;
display:inline-block;
}
#reset {
width:25%;
height:7%;
font-size:16px;
background-color:salmon;
border:0;
display:inline-block;
float:right;
}
html, body {
width:100%;
height:100%;
}
</style>
</body>
</html>