Practice 3
Build a small attendance register from an array of student objects. The array is the source of truth: event handlers change the objects, then render() updates the page.
Download the starter and open the folder in VS Code (on Windows: extract the ZIP, then double-click vscode-win.bat). Open example.html with Live Server first: it contains the final rendered HTML so you can see what you are building. Then open index.html and write all JavaScript in script.js. The data and the DOM selections are already provided; you will declare the functions and attach the event listeners yourself.
Kezdőcsomag
A feladatleírás és a kezdőfájlok helyi munkához előkészítve.
Tartalmazott fájlok: example.htmlindex.htmlscript.jsstyle.css
Feladatok
Declare a function named studentHtml(student) that returns the HTML for one student.
Each student must become an li containing:
- a
data-neptunattribute with the student’s Neptun code; - the class
presentwhenstudent.attendanceistrue, otherwise no class; - a checkbox that has the
checkedattribute when the student is present; - the student’s name and Neptun code.
For Charlie, the generated HTML should have this structure:
<li data-neptun="M9N3PQ" class="present">
<label>
<input type="checkbox" checked>
Charlie (M9N3PQ)
</label>
</li>
Use the ternary operator for the conditional class and checked attribute.
Declare render(). It must:
- transform every student with
map(studentHtml); - join the HTML strings with
join(""); - write the result into
list.innerHTML; - count the present students with
filter(...).length; - display text such as
2 of 5 presentinsummary.
Call render() once so the initial list appears when the page loads.
The Everyone is present button uses a normal, direct event handler.
Declare handleAllPresent(). Inside it:
- use
forEachto set every student’sattendanceproperty totrue; - call
render().
Attach the function to the button’s click event with addEventListener. Pass the function itself without parentheses so the browser calls it when the click happens.
Expected result: clicking the button checks every checkbox, colors every item green, and updates the summary.
Do not attach a separate listener to every checkbox. Attach one input listener to the shared parent, list.
Declare handleAttendance(event). Inside it:
- use
event.target.matches("input")to make sure the event came from a checkbox; - use
event.target.closest("li")to find the student’s list item; - read the Neptun code from
li.dataset.neptun; - use
findto locate the corresponding object instudents; - copy
event.target.checkedinto the object’sattendanceproperty; - call
render().
Attach handleAttendance to the input event of list.
Here event.target is the checkbox that changed, while event.currentTarget is the ul that owns the listener. The event reaches the ul because it bubbles upward through the checkbox’s parents.
Expected result: changing any checkbox updates the object, the item color, and the summary.
Declare handleSubmit(event) and attach it to the form’s submit event.
Inside the handler:
- call
event.preventDefault()so the browser does not reload the page; - read
neptunInput.valueandnameInput.value; - stop the function if either value is empty;
- push a new object into
studentswith the entered Neptun code and name, andattendance: false; - clear both input fields;
- call
render().
Expected result: the new student appears as absent. Its checkbox must work immediately without registering another listener. This works because the input listener belongs to the parent ul, which stays on the page when render() creates new list items.
Követelmények
- Change only
script.js. - Keep
studentsas the source of truth: change the objects first, then callrender(). - Generate the student list from the array; do not write student
lielements intoindex.html. - Use one delegated
inputlistener onlist, not one listener per checkbox. - Attach events with
addEventListener; do not use inline HTML event attributes. - The initial render, the button, every checkbox, and the add form must all work without Console errors.