Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
tetyana-shatrova committed Oct 5, 2023
1 parent be580c2 commit 714dc69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/stylesString.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const stylesString = `
padding-left : 18px;
padding-right: 30px;
;
;
position: relative;
Expand Down

0 comments on commit 714dc69

Please sign in to comment.