-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
add task solution #1287
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
if (string.charAt(0).toUpperCase() === string.charAt(0)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
input.setAttribute('placeholder', capitalizedWords); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
parentInput.insertBefore(label, input); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}); |
There was a problem hiding this comment.
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 ofstr
. You can directly manipulatestr
in your function. This would make your code cleaner and more efficient.