From 962657ec15d9a85fabb05ecb18b7cbdba26b0f42 Mon Sep 17 00:00:00 2001 From: "Hlib Vakhrameev@icloud.com" Date: Tue, 3 Oct 2023 16:28:46 +0300 Subject: [PATCH] Solution --- src/convertToObject.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..0b1e9c2b9 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -3,16 +3,32 @@ /** * Implement convertToObject function: * - * Function takes a string with styles (see an example in [stylesString.js](./stylesString.js)) +* Function takes a string with styles + (see an example in [stylesString.js](./stylesString.js)) * and returns an object where CSS properties are keys - * and values are the values of related CSS properties (see an exampl in [test file](./convertToObject.test.js)) + * and values are the values of related CSS properties + * (see an exampl in [test file](./convertToObject.test.js)) * * @param {string} sourceString * * @return {object} */ function convertToObject(sourceString) { - // write your code here + const cssArr = sourceString + .split(';') + .filter(style => style.trim() !== ''); + + const cssObject = {}; + + cssArr.forEach(style => { + const [property, value] = style + .split(':') + .map(part => part.trim()); + + cssObject[property] = value; + }); + + return cssObject; } module.exports = convertToObject;