-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Solution #3825
base: master
Are you sure you want to change the base?
Solution #3825
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 |
---|---|---|
|
@@ -7,6 +7,33 @@ | |
*/ | ||
function convertToObject(sourceString) { | ||
// write your code here | ||
// Видалення зайвих пробілів та порожніх рядків | ||
const trimmedString = sourceString.trim(); | ||
|
||
// Розбиваємо рядок на декларації за допомогою крапки з комою | ||
const rules = trimmedString.split(';'); | ||
|
||
const result = {}; | ||
// Обробляємо кожне правило | ||
|
||
rules.forEach((rule) => { | ||
const trimmedRule = rule.trim(); | ||
// Очищаємо зайві пробіли з початку та кінця кожного правила | ||
|
||
if (trimmedRule) { | ||
// Перевіряємо, чи не порожній рядок | ||
// Розбиваємо на властивість та значення | ||
const [property, value] = trimmedRule | ||
.split(':') | ||
.map((item) => item.trim()); | ||
Comment on lines
+26
to
+28
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. The split operation on line 27 assumes that each rule contains exactly one colon. If there are multiple colons within a rule, this will not work as expected. According to the additional prompt, handling multiple colons is not required, but it's something to keep in mind if you encounter unexpected input. |
||
|
||
if (property && value) { | ||
result[property] = value; // Додаємо до об'єкта | ||
} | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
module.exports = convertToObject; |
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.
The current implementation splits the string by semicolons, which is correct. However, ensure that the input string is properly formatted, as the function does not handle cases with multiple semicolons without any styles between them. This is acceptable according to the additional prompt, but be aware of potential empty strings in the
rules
array.