-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolutions.js
More file actions
303 lines (283 loc) Β· 11.4 KB
/
solutions.js
File metadata and controls
303 lines (283 loc) Β· 11.4 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
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
293
294
295
296
297
298
299
300
301
302
303
// ============================================================
// solutions.js β SnapGrid Social Media Dashboard
// Assignment 3: JavaScript DOM Manipulation & Event Handling
//
// Name: ___________________________
// Date: ___________________________
// ============================================================
//
// INSTRUCTIONS:
// β’ Write all your code in this file β do not modify index.html or styles.css
// β’ Test each task in the browser before moving on
// β’ Recommended order: Tasks 1 β 2 β 4 β 3 β 5
// (Task 3's Post Composer calls Task 4's attachLikeListeners(),
// so it helps to have Task 4 written first)
// ============================================================
// ============================================================
// TASK 1 β Dark / Light Mode Toggle (3 points)
//
// TODO:
// 1. Select #themeToggleBtn, #themeIcon, and #themeLabel
// 2. Declare: let isDark = true
// 3. Add a 'click' event listener to the button
// 4. Inside the listener:
// a. Flip isDark using !isDark
// b. If isDark is now true:
// - document.documentElement.setAttribute('data-theme', 'dark')
// - Set #themeIcon text to 'π'
// - Set #themeLabel text to 'Dark Mode'
// c. If isDark is now false:
// - Set data-theme attribute to 'light'
// - Set #themeIcon text to 'βοΈ'
// - Set #themeLabel text to 'Light Mode'
// ============================================================
// YOUR CODE HERE
const themeBtn = document.getElementById("themeToggleBtn");
let isDark = true;
themeBtn.addEventListener("click", () => {
isDark = !isDark;
if (isDark) {
document.documentElement.setAttribute("data-theme", "dark");
document.getElementById("themeIcon").textContent = "π";
document.getElementById("themeLabel").textContent = "Dark Mode";
} else {
document.documentElement.setAttribute("data-theme", "light");
document.getElementById("themeIcon").textContent = "βοΈ";
document.getElementById("themeLabel").textContent = "Light Mode";
}
});
// ============================================================
// TASK 2 β Follow / Unfollow with Live Counter (4 points)
//
// TODO:
// 1. Select #followBtn, #followBtnText, and #followerCount
// 2. Declare: let isFollowing = false
// let followers = 1284
// 3. Add a 'click' event listener to #followBtn
// 4. Inside the listener:
// a. Flip isFollowing
// b. If now following:
// - followers++
// - Set #followBtnText to 'β
Following'
// - Add class 'following' to #followBtn
// c. If now unfollowed:
// - followers--
// - Set #followBtnText to 'β Follow'
// - Remove class 'following' from #followBtn
// d. In both cases, update #followerCount.textContent
// to followers.toLocaleString()
// ============================================================
// YOUR CODE HERE
const followBtn = document.getElementById("followBtn");
let isFollowing = false;
let followers = 1284;
followBtn.addEventListener("click", () => {
isFollowing = !isFollowing;
if (isFollowing) {
followers++;
document.getElementById("followBtnText").textContent = "β
Following";
followBtn.classList.add("following");
} else {
followers--;
document.getElementById("followBtnText").textContent = "β Follow";
followBtn.classList.remove("following");
}
document.getElementById("followerCount").textContent =
followers.toLocaleString();
});
// ============================================================
// TASK 3 β Live Post Composer (5 points)
// β Tip: Complete Task 4 first β you need to call
// attachLikeListeners() inside this task.
//
// TODO:
// 1. Select #composerTextarea, #charCounter, #postBtn,
// #postFeed, and #postCount
//
// 2. Add an 'input' event listener to #composerTextarea:
// a. remaining = 280 - textarea.value.length
// b. Update #charCounter text to "X characters remaining"
// c. If remaining < 20, add class 'counter-warning'
// to #charCounter; otherwise remove it
// d. Disable #postBtn if textarea value is empty (after trim);
// enable it otherwise
//
// 3. Add a 'click' event listener to #postBtn:
// a. Get postText = composerTextarea.value.trim()
// b. Create a new <article> element (document.createElement)
// - Add classes: post-card new-post
// - Set data-tags attribute (extract hashtags from text or leave "")
// c. Set its innerHTML using the template from the task instructions
// (remember to drop postText into the <p class="post-body">)
// d. postFeed.prepend(newPost)
// e. Call attachLikeListeners() so the new heart button works
// f. Reset the textarea, counter text, remove counter-warning,
// disable the post button
// g. Increment and update #postCount
// ============================================================
// YOUR CODE HERE
const composerTextarea = document.getElementById("composerTextarea");
const charCounter = document.getElementById("charCounter");
const postBtn = document.getElementById("postBtn");
const postFeed = document.getElementById("postFeed");
const postCount = document.getElementById("postCount");
composerTextarea.addEventListener("input", () => {
const remaining = 280 - composerTextarea.value.length;
charCounter.textContent = `${remaining} characters remaining`;
if (remaining < 20) {
charCounter.classList.add("counter-warning");
} else {
charCounter.classList.remove("counter-warning");
}
postBtn.disabled = composerTextarea.value.trim() === "";
});
postBtn.addEventListener("click", () => {
const postText = composerTextarea.value.trim();
const newPost = document.createElement("article");
newPost.classList.add("post-card", "new-post");
const hashtags = postText.match(/#\w+/g);
newPost.setAttribute("data-tags", hashtags ? hashtags.join(",") : "");
newPost.innerHTML = `
<div class="post-header">
<div class="post-avatar">π§βπ»</div>
<div class="post-meta">
<span class="post-author">Alex Rivera</span>
<span class="post-time">Just Now</span>
</div>
</div>
<p class="post-body">${postText}</p>
<button class="like-btn" data-liked="false">
<span class="like-icon">π€</span>
<span class="like-count">0</span>
</button>
`;
postFeed.prepend(newPost);
attachLikeListeners();
composerTextarea.value = "";
charCounter.textContent = "280 characters remaining";
charCounter.classList.remove("counter-warning");
postBtn.disabled = true;
postCount.textContent = parseInt(postCount.textContent, 10) + 1;
});
// ============================================================
// TASK 4 β Like / Unlike Posts (4 points)
// β Write this as a NAMED FUNCTION β Task 3 calls it.
//
// TODO:
// 1. Define: function attachLikeListeners() { ... }
// 2. Inside it:
// a. Select all .like-btn elements
// b. Loop with forEach
// c. For each btn, clone it to remove old listeners:
// const fresh = btn.cloneNode(true);
// btn.parentNode.replaceChild(fresh, btn);
// d. Add a 'click' listener to 'fresh':
// - Read liked state:
// const alreadyLiked = fresh.getAttribute('data-liked') === 'true'
// - Get count from .like-count span inside the button (parseInt)
// - If alreadyLiked:
// set data-liked to 'false', remove class 'liked',
// icon β 'π€', count - 1
// - Else:
// set data-liked to 'true', add class 'liked',
// icon β 'β€οΈ', count + 1
// - Call updateTotalLikes()
//
// 3. Define: function updateTotalLikes() { ... }
// a. Count all .like-btn[data-liked="true"] elements
// b. Update #totalLikedCount with that number
//
// 4. Call attachLikeListeners() at the bottom of this section
// to wire up the initial posts.
// ============================================================
// YOUR CODE HERE
function attachLikeListeners() {
const likeButtons = document.querySelectorAll(".like-btn");
likeButtons.forEach((btn) => {
const fresh = btn.cloneNode(true);
btn.parentNode.replaceChild(fresh, btn);
fresh.addEventListener("click", () => {
const alreadyLiked = fresh.getAttribute("data-liked") === "true";
const likeCountSpan = fresh.querySelector(".like-count");
const count = likeCountSpan
? parseInt(likeCountSpan.textContent, 10) || 0
: 0;
if (alreadyLiked) {
fresh.setAttribute("data-liked", "false");
fresh.classList.remove("liked");
const icon = fresh.querySelector(".like-icon");
if (icon) icon.textContent = "π€";
if (likeCountSpan) likeCountSpan.textContent = count - 1;
} else {
fresh.setAttribute("data-liked", "true");
fresh.classList.add("liked");
const icon = fresh.querySelector(".like-icon");
if (icon) icon.textContent = "β€οΈ";
if (likeCountSpan) likeCountSpan.textContent = count + 1;
}
updateTotalLikes();
});
});
}
function updateTotalLikes() {
const totalLiked = document.querySelectorAll(
'.like-btn[data-liked="true"]',
).length;
document.getElementById("totalLikedCount").textContent = totalLiked;
}
attachLikeListeners();
// ============================================================
// TASK 5 β Hashtag Filter (4 points)
//
// TODO:
// 1. Select all .tag-pill elements and #filterResultMsg
// 2. Loop through the pills with forEach and add a 'click'
// listener to each
// 3. Inside the listener:
// a. Remove class 'active' from ALL pills, then add it to
// the clicked one only
// b. Get the tag: pill.getAttribute('data-tag')
// c. Get all .post-card elements inside #postFeed
// Use Array.from() so you can use .filter() later
// d. Loop through posts:
// - If tag === 'all': remove class 'hidden' from every post
// - Otherwise: check if post's data-tags includes(tag)
// yes β remove 'hidden'
// no β add 'hidden'
// e. Count visible posts:
// posts.filter(p => !p.classList.contains('hidden')).length
// f. Update #filterResultMsg:
// 'all' tag β "Showing all X posts"
// other tags β "X post(s) tagged #tagname"
// ============================================================
// YOUR CODE HERE
const tagPills = document.querySelectorAll(".tag-pill");
const filterResultMsg = document.getElementById("filterResultMsg");
tagPills.forEach((pill) => {
pill.addEventListener("click", () => {
tagPills.forEach((p) => p.classList.remove("active"));
pill.classList.add("active");
const tag = pill.getAttribute("data-tag");
const posts = Array.from(document.querySelectorAll("#postFeed .post-card"));
posts.forEach((post) => {
if (tag === "all") {
post.classList.remove("hidden");
} else {
const postTags = post.getAttribute("data-tags").split(",");
if (postTags.includes(tag)) {
post.classList.remove("hidden");
} else {
post.classList.add("hidden");
}
}
});
const visibleCount = posts.filter(
(p) => !p.classList.contains("hidden"),
).length;
if (tag === "all") {
filterResultMsg.textContent = `Showing all ${visibleCount} posts`;
} else {
filterResultMsg.textContent = `${visibleCount} post(s) tagged #${tag}`;
}
});
});