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

Solution #3825

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@
*/
function convertToObject(sourceString) {
// write your code here
// Видалення зайвих пробілів та порожніх рядків
const trimmedString = sourceString.trim();

// Розбиваємо рядок на декларації за допомогою крапки з комою
const rules = trimmedString.split(';');

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.


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

Choose a reason for hiding this comment

The 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;
Loading