Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
odolga committed Oct 31, 2023
1 parent be580c2 commit 55f6cac
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@
/**
* 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
let newString = '';

for (let i = 0; i < sourceString.length; i++) {
if (((sourceString[i] === '\n')
|| ((sourceString[i] === ' ') && (sourceString[i + 1] === ' '))
|| ((sourceString[i] === ' ') && (sourceString[i - 1] === ' '))
|| ((sourceString[i] === ' ') && (sourceString[i - 1] === ':'))
|| ((sourceString[i] === ' ') && (sourceString[i - 1] === ';')))) {
continue;
} else {
newString += sourceString[i];
}
}

const result = newString.split(/;|:/)
.filter((element) => element.length > 0);
const newObj = {};

for (let i = 0; i < result.length; i += 2) {
newObj[result[i]] = result[(i + 1)];
}

return newObj;
}

module.exports = convertToObject;

0 comments on commit 55f6cac

Please sign in to comment.