Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #3793

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
*
* @return {object}
*/
function convertToObject(sourceString) {
// write your code here
function convertToObject(styleStr) {
const styleObj = {};
const styleArr = styleStr.split(';');

styleArr.forEach((style) => {
const [key, value] = style.split(':').map((s) => s.trim());

styleObj[key] = value;
});
Comment on lines +12 to +16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a check to ensure that both key and value are not undefined or empty. This will prevent adding invalid entries to the styleObj when the style string contains empty segments or malformed key-value pairs.


return styleObj;
}

module.exports = convertToObject;
Loading