feat: parse list with data attributes#1975
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am asking for some changes before this can be considered complete, because there are a couple of critical requirement and robustness issues remaining.
Critical issues to fix:
- In the repository,
README.mdwas modified even though the task explicitly says to change onlymain.js; please revert any unintended README changes so that onlysrc/scripts/main.jsis altered for this task. - In
src/scripts/main.js, the helper functiontoNumbercurrently callsreplaceAlldirectly on its argument, and bothsortListandgetEmployeespass values likea?.dataset?.salaryanditem?.dataset?.age, which can beundefined; this can throw a runtime TypeError when a dataset attribute is missing and does not meet the safe-conversion requirements [CHECKLIST ITEM #15][CHECKLIST ITEM #16]. To fix this, maketoNumbertolerant of non-string input (e.g., by wrapping the argument withString(str ?? '')before callingreplaceAll) so it always returns a valid number even when data attributes are absent. - Near the bottom of
main.js, you doconst listElement = document.querySelector('ul')and immediately callsortList(listElement)andgetEmployees(listElement); if the<ul>is not present,listElementwill benulland these calls will throw, so please add a simple null check/guard before invoking these functions.
As minor (non-blocking) improvements, you might consider using a more specific selector than 'ul' and optionally capturing or exporting the result of getEmployees, but these are not required for acceptance. Your core logic for sorting the list and mapping employees looks good; once the above robustness and checklist-related issues are addressed, this solution will be in good shape.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| 'use strict'; | ||
|
|
||
| // write code here | ||
| function toNumber(str) { |
There was a problem hiding this comment.
The helper toNumber assumes str is a string and calls replaceAll on it. If str is undefined/null this will throw. Make toNumber tolerant of non-string input (e.g. coerce with String(str ?? '') or use (str ?? '').replaceAll(...)) so it always returns a number as required by the checklist.
|
|
||
| // write code here | ||
| function toNumber(str) { | ||
| const num = Number(str.replaceAll(/[^0-9]/g, '')); |
| const aValue = toNumber(a?.dataset?.salary); | ||
| const bValue = toNumber(b?.dataset?.salary); |
There was a problem hiding this comment.
In sortList you use optional chaining and pass possibly undefined values to toNumber: toNumber(a?.dataset?.salary) and toNumber(b?.dataset?.salary). Either ensure toNumber handles undefined (recommended) or provide a default here (e.g. toNumber(a?.dataset?.salary ?? '')). This prevents runtime errors when a dataset entry is missing.
| salary: toNumber(item?.dataset?.salary), | ||
| age: toNumber(item?.dataset?.age), |
There was a problem hiding this comment.
Similarly, in getEmployees you call toNumber(item?.dataset?.salary) and toNumber(item?.dataset?.age). Ensure toNumber handles undefined or provide defaults here (e.g. toNumber(item?.dataset?.salary ?? '')) so the returned objects always contain numeric salary and age per the schema.
| const listElement = document.querySelector('ul'); | ||
|
|
||
| sortList(listElement); | ||
| getEmployees(listElement); |
There was a problem hiding this comment.
You call document.querySelector('ul') and then immediately call sortList(listElement) and getEmployees(listElement) without checking if the element actually exists. Add a null-check (or guard) before calling these functions to avoid runtime errors when the list isn't present.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
This requires changes because, although your sortList and getEmployees implementations in src/scripts/main.js correctly satisfy the functional requirements (sorting by salary descending, parsing employee objects, and safely handling dataset values), you also modified README.md even though the task explicitly says that only src/scripts/main.js should be changed for this assignment. Changing README.md (in the section with the DEMO LINK near the top of the file) violates that constraint and can interfere with automated grading or consistency checks for the repository. Please revert README.md to its original state in this PR so that the only code changes come from src/scripts/main.js; if the demo link needs to be updated for a different step of the course, do that in the appropriate task where README edits are allowed. Once you revert that file, your JavaScript solution looks solid: it correctly uses data attributes, sorts in descending order by salary, converts strings to numbers robustly, and guards against a missing <ul> element. As a small, non-blocking improvement, consider using a more specific selector than document.querySelector('ul') and think about how the returned array from getEmployees might be exposed or used if tests rely on it. Overall, your core logic is well implemented; align the file changes with the task constraints and you should be in great shape.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
|
@mateacademy-ai-mentor README.md was not intentionally changed. Those changes were introduced automatically by formatting tools during the commit process (likely Prettier adjusting commas and spacing). I did not edit this file manually, and it is not related to the task. All required logic is implemented only in If necessary, I can try to revert the README.md file, but the formatting tools may apply the same changes again. |
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your solution is approved as an educational override so you can continue progressing while you address a couple of remaining issues. The JavaScript in src/scripts/main.js is well done: sortList correctly sorts by numeric salary in descending order, getEmployees returns objects with the required { name, position, salary, age } shape, and your toNumber helper safely converts salary strings to numbers. The main issue is that README.md has been modified in this PR, which violates the requirement that only src/scripts/main.js be changed and can interfere with automated grading [CHECKLIST ITEM #13]; in the diff view, every line of README.md shown as changed (with its line numbers) should be restored to exactly match the upstream main branch. To fix this, use one of the approaches you mentioned, such as git checkout origin/main -- README.md or git restore --source=origin/main README.md, then commit (using --no-verify if a formatter keeps reapplying changes). As a minor improvement, you could store or expose the result of getEmployees(listElement) (for example, assigning it to a variable or window.employees) so tests or other scripts can easily access the employee data [CHECKLIST ITEM #7]. Overall, your JavaScript meets the functional requirements nicely—once you revert the README changes and re-run the tests, your solution will be fully aligned with the task expectations.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
No description provided.