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 #3682

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
* @return {object}
*/
function convertToObject(sourceString) {
// write your code here
let res = sourceString.split(';');

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 handle cases where sourceString might be empty or not a string. This can prevent potential runtime errors.


res = res.map((el) => el.split(':'));

Choose a reason for hiding this comment

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

Ensure that the input string is correctly formatted. If the string does not contain the expected delimiter ':', the resulting object might not be as intended.

res = res.map((el) => el.map((item) => item.trim()));
res = res.filter((item) => item.length > 1);

Choose a reason for hiding this comment

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

Filtering items with item.length > 1 is a good approach to remove invalid entries. However, consider logging or handling these cases to inform the user about ignored entries.

Choose a reason for hiding this comment

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

  • res/result/el/elem/obj/object/arr/array and etc are bad names, name should describe variable
  • Prefer const over let, you can call all iteration method in chain


res = res.reduce((acc, item) => {
return {
...acc,
[item[0]]: item[1],
};
}, {});

return res;
}

module.exports = convertToObject;
Loading