diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..395fd7108 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -3,16 +3,35 @@ /** * 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 array = sourceString.split(';'); + const arrayRes = array + .map((item) => item.trim()) + .filter((item) => item !== '') + .map((item) => item.split(':')); + + arrayRes.forEach((item) => { + item[0] = item[0].trim(); + item[1] = item[1].trim(); + }); + + const res = {}; + + for (let i = 0; i < arrayRes.length; i++) { + res[arrayRes[i][0]] = arrayRes[i][1]; + } + + return res; } module.exports = convertToObject; diff --git a/src/stylesString.js b/src/stylesString.js index e1db486c1..248e8fa94 100644 --- a/src/stylesString.js +++ b/src/stylesString.js @@ -19,7 +19,7 @@ const stylesString = ` padding-left : 18px; padding-right: 30px; ; - + ; position: relative;