From 60bd2b17d45b3c301ac1d3045e8301212f22a5df Mon Sep 17 00:00:00 2001 From: Olga Tsyb Date: Tue, 17 Oct 2023 12:55:37 +0300 Subject: [PATCH] Solution --- src/convertToObject.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..7e19b7ecd 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -1,18 +1,19 @@ 'use strict'; -/** - * Implement convertToObject function: - * - * 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)) - * - * @param {string} sourceString - * - * @return {object} - */ function convertToObject(sourceString) { - // write your code here + const noTransfer = sourceString.replace(/\n/g, ''); + const sourceArr = noTransfer.split(';'); + const result = {}; + + for (let i = 0; i < sourceArr.length; i++) { + const arrayValue = sourceArr[i].split(':'); + + if (arrayValue[1] !== undefined) { + result[arrayValue[0].trim()] = arrayValue[1].trim(); + } + } + + return result; } module.exports = convertToObject;