From ad5953c709b2fbf44fdbbf7cedb2ec6518f11fe3 Mon Sep 17 00:00:00 2001 From: Bohdan Poliienko Date: Mon, 23 Dec 2024 15:07:05 +0200 Subject: [PATCH] solution --- src/convertToObject.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 32c556ea4..dc87d1d88 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -6,7 +6,22 @@ * @return {object} */ function convertToObject(sourceString) { - // write your code here + if (sourceString.length === 0) { + return {}; + } + + const convertedOblect = sourceString + .split(';') + .filter((obj) => obj.length > 0) + .reduce((acc, obj) => { + const [key, value] = obj.split(':').map((part) => part.trim()); + + acc[key] = value; + + return acc; + }, {}); + + return convertedOblect; } module.exports = convertToObject;