Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_task_fix_form_DOM/)
- [DEMO LINK](https://TiFAu.github.io/js_task_fix_form_DOM/)
2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
Expand Down
23 changes: 22 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
'use strict';

// write code here
function addingPlaceholderAndLabel(element, index, arr) {
const elementLabel = document.createElement('label');

if (element.name) {
element.placeholder =
element.name.charAt(0).toUpperCase() + element.name.slice(1);
Comment on lines +7 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic does not correctly handle input names containing a hyphen, which was the main point of the previous review. For a name like confirm-password, this produces Confirm-password. You need to first replace the hyphen with a space and then capitalize the string to get the required Confirm password.

}

elementLabel.className = 'field-label';

if (!element.id && element.name) {
elementLabel.htmlFor = element.name;
}
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional block is made ineffective by the unconditional assignment on line 17, which overwrites anything set here. The requirement is to set the for attribute to the input's id, which line 17 already handles.


elementLabel.htmlFor = element.id;
elementLabel.textContent = element.name.toUpperCase();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the placeholder, this doesn't handle multi-word name attributes correctly. A name like confirm-password would become CONFIRM-PASSWORD, but the expected result is CONFIRM PASSWORD. You'll need to replace special characters with spaces.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text for the label should also have hyphens replaced with spaces. Additionally, based on the expected result image, it seems only the first letter should be capitalized, similar to the placeholder text (e.g., Confirm password instead of CONFIRM-PASSWORD).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the placeholder, this doesn't handle hyphens correctly. For confirm-password, this code will produce CONFIRM-PASSWORD instead of the expected CONFIRM PASSWORD. The hyphen should be replaced with a space before converting the text to uppercase.

element.parentElement.prepend(elementLabel);
}

const allInputs = [...document.querySelectorAll('form input')];

allInputs.forEach(addingPlaceholderAndLabel);
Loading