-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
237 lines (200 loc) · 8.56 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learning Javascript with HTML</title>
</head>
<body>
<section>
<hr> <!-- you can ignore this; <hr> is a tag used to style and separate sections -->
<h1 style="color: teal; text-align: center;">Welcome to the Javascript Foundations Lab!</h1>
<hr>
<h1 style="color: green; text-align: center;">Challenge 3.1 | Example</h1>
<!--Challenge 3.1 | Example-->
<div id="myDiv">Hello</div>
<p class="myP">Paragraph</p>
<h1>Heading</h1>
<hr>
</section>
<section>
<h1 style="color: blue; text-align: center;">Challenge 3.1 | Your Turn</h1>
<!--Challenge 3.1 | Your Turn-->
<div id="anotherDiv">Hello</div>
<p class="anotherP">Paragraph</p>
<script src="index.js"></script>
<hr>
<!-- you can ignore this; this is for styling purposes -->
<h1 style="color: green; text-align: center;">Challenge 3.3.1 | Example</h1>
</section>
<section>
<hr>
<h1 style="color: green; text-align: center;">Challenge 3.2 | Example</h1>
<!-- Challenge 3.2 | Example -->
<button id="changeContentBtn">Change The Content Below</button>
<div id="changeContentDiv"><h4>Original Content</h4></div>
<script>
document.getElementById("changeContentBtn").addEventListener("click", function() {
document.getElementById("changeContentDiv").textContent = "Content Changed!";
});
</script>
<hr>
</section>
<section>
<h1 style="color: blue; text-align: center;">Challenge 3.2 | Your Turn</h1>
<!-- Challenge 3.2 | Your Turn -->
<button id="changeAnotherContentBtn">Change The Content Below</button>
<div id="anotherContentDiv"><h4>Your Original Content</h4></div>
<script>
// Your code here
</script>
<hr>
</section>
<section>
<h1 style="color: green; text-align: center;">Challenge 3.3.2 | Example</h1>
<!-- Challenge 3.3.2 | Example-->
<form id="myForm">
<input type="text" id="itemInput">
<button type="submit">Add Item</button>
</form>
<ul id="itemList"><strong><em>Grocery List</em></strong></ul>
<script>
// Pre-add some items to the list for illustrative purposes [THIS IS JUST FOR THE EXAMPLE, not necessary].
const initialItems = ["Milk", "Eggs", "Bread"];
// Loops through the given array InitialItems to add its elements to the list.
initialItems.forEach(item => {
let listItem = document.createElement("li");
listItem.textContent = item;
document.getElementById("itemList").appendChild(listItem);
});
// Code to actually add items to the list.
document.getElementById("myForm").addEventListener("submit", function(event) {
event.preventDefault();
// Gets the value inputted by the user into the input field.
let item = document.getElementById("itemInput").value;
if (item.trim() !== '') { // Ensures the input is not empty or just spaces. ".trim" removes all whitespaces.
// Creates an <li> element (a list item element).
let listItem = document.createElement("li");
// Sets the text of the list item element to the item value inputted by the user.
listItem.textContent = item;
// Adds the list item element to the DOM.
document.getElementById("itemList").appendChild(listItem);
// Clears the input field after submission.
document.getElementById("itemInput").value = '';
}
});
</script>
<hr>
</section>
<section>
<h1 style="color: green; text-align: center;">Challenge 4.1 | Example</h1>
<!-- Challenge 4.1 | Example -->
<div style="text-align: center;">
<button id="alertBtn" style="font-size: 20px; padding: 15px 30px;">Click Me!</button>
</div>
<script>
document.getElementById("alertBtn").addEventListener("click", function() {
alert("Button Clicked!");
});
</script>
<hr>
</section>
<section>
<h1 style="color: blue; text-align: center;">Challenge 4.1 | Your Turn</h1>
<!-- Challenge 4.1 | Your Turn -->
<!-- Here's a resource for a potential hint: https://rb.gy/2vl4fs -->
<div id="colorDiv" style="width:100px; height:100px; background-color:lightblue;"></div>
<script>
// Your code here
</script>
<button id="colorBtn">Change Color</button>
<hr>
</section>
<section>
<h1 style="color: green; text-align: center;">Challenge 4.1.2 | Example</h1>
<!-- Challenge 4.1.2 | Example -->
<div id="clickArea" style="width:300px; height:300px; border:1px solid black;"></div>
<script>
document.getElementById("clickArea").addEventListener("click", function(event) {
console.log("X: " + event.clientX + ", Y: " + event.clientY);
});
</script>
<hr>
</section>
<section>
<h1 style="color: green; text-align: center;">Challenge 5.1 | Example</h1>
<!-- Challenge 5.1 | Example -->
<button id="handleAsyncButton">Get Promise Result</button>
<h4 id="result">[Promise Result Here]</h4>
<script>
function asyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
let success = true;
if (success) {
resolve('Operation was successful | Promise resolved!');
} else {
reject('Operation failed | Promise rejected!');
}
}, 1000);
});
}
document.getElementById('handleAsyncButton').addEventListener('click', () => {
asyncFunction()
.then(message => {
document.getElementById('result').innerText = message;
})
.catch(error => {
document.getElementById('result').innerText = error;
});
});
</script>
<hr>
</section>
<section>
<h1 style="color: blue; text-align: center;">Challenge 5.1 | Your Turn</h1>
<!-- Challenge 5.1 | Your Turn -->
<button id="handleYourAsyncButton">Get Your Promise Result</button>
<h4 id="yourResult">[Your Promise Results Here]</h4>
<script>
function yourAsyncFunction() {
// Your code here
}
document.getElementById('handleYourAsyncButton').addEventListener('click', () => {
// Your Code here
});
</script>
<hr>
</section>
<section>
<h1 style="color: green; text-align: center;">Challenge 5.2.1 | Example</h1>
<!-- Challenge 5.2.1 | Example -->
<button id="fetchRawDataButton">Fetch Raw Data</button>
<h3>Raw Data</h3>
<div id="rawDataDisplay"><h4>[This will display the raw, unprocessed data in JSON format from the API endpoint.]</h4></div>
<script>
document.getElementById('fetchRawDataButton').addEventListener('click', fetchRawData);
</script>
<hr>
<h1 style="color: green; text-align: center;">Challenge 5.2.2 | Example</h1>
<!-- Challenge 5.2.2 | Example -->
<button id="fetchDataButton">Fetch Formatted Data</button>
<h3>Formatted Data</h3>
<div id="dataDisplay"><h4>[This will display the fetched API data.]</h4></div>
<script>
document.getElementById('fetchDataButton').addEventListener('click', fetchData);
</script>
<hr>
</section>
<section>
<h1 style="color: blue; text-align: center;">Challenge 5.2.2 | Your Turn</h1>
<!-- Challenge 5.2.2 | Your Turn -->
<button id="fetchYourDataButton">Fetch Data</button>
<div id="yourDataDisplay"><h4>[Your Fetched Data Here]</h4></div>
<script>
document.getElementById('fetchYourDataButton').addEventListener('click', fetchYourData);
</script>
<hr>
</section>
</body>
</html>