Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #1287

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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://VasylPylypchynets.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
40 changes: 40 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
'use strict';

// write code here
const fieldText = document.querySelectorAll('.field-text');

function splitCamelCase(str) {
let string = str;

Choose a reason for hiding this comment

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

There is no need to create a new variable string that is a copy of str. You can directly manipulate str in your function. This would make your code cleaner and more efficient.


if (string.charAt(0).toUpperCase() === string.charAt(0)) {

Choose a reason for hiding this comment

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

This line checks if the first character of the string is uppercase. This is not wrong, but it might be more clear to other developers if you use a regular expression to check this. For example, you can use if (/^[A-Z]/.test(string)) to achieve the same result.

string = ' ' + string;
}

return string.replace(/([a-z])([A-Z])/g, '$1 $2').trim();
}

function capitalizeWords(str) {
if (str === null || str === undefined) {
return '';
}

return str
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}

fieldText.forEach((input) => {
const InputName = 'name';
const label = document.createElement('label');
const nameInput = input.getAttribute(InputName)
? splitCamelCase(input.getAttribute(InputName))
: null;
const capitalizedWords = capitalizeWords(nameInput);
const parentInput = input.parentNode;

label.classList.add('field-label');
label.setAttribute('for', input.getAttribute(InputName));
label.textContent = nameInput.toUpperCase();

Choose a reason for hiding this comment

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

You are setting the label's text content to the uppercase version of nameInput. However, you have already created a capitalized version of nameInput in the capitalizedWords variable. It would be more efficient to use capitalizedWords here instead of calling toUpperCase() again.


input.setAttribute('placeholder', capitalizedWords);

Choose a reason for hiding this comment

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

In this line, you are setting the input's placeholder to the capitalized version of nameInput. However, if nameInput is null, capitalizeWords will return an empty string, and your input will have an empty placeholder. You should add a condition to check if nameInput is not null before setting the placeholder.


parentInput.insertBefore(label, input);

Choose a reason for hiding this comment

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

You are inserting the label before the input field. Usually, it's more common to have the label before the input field in the HTML structure for better accessibility. If you want to change the visual order, consider using CSS.

});
Loading