From efa2ecd4efd11f48821d5db677ada26d718e98e4 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Wed, 3 Apr 2024 17:24:28 +0300 Subject: [PATCH 01/18] added initial solution --- src/inverseRobot.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 4906e020..0412aa28 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -3,11 +3,25 @@ /* * @param {object} robot * - * @return {object} + * @return {object | null} */ function inverseRobot(robot) { - // write code here + const fixed = {}; + const valuesSet = new Set(); + + for (const key in robot) { + const value = robot[key]; + + if (valuesSet.has(value)) { + return null; + } + + fixed[value] = key; + valuesSet.add(value); + } + + return fixed; } module.exports = inverseRobot; From a946d530999cdbe000662d84b60bb1bc7806a3b9 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Thu, 4 Apr 2024 20:56:21 +0300 Subject: [PATCH 02/18] solution 2 --- src/inverseRobot.js | 46 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 0412aa28..cdc084d2 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -1,27 +1,43 @@ 'use strict'; -/* +/** + * Situs inversus + * Transposition of internal organs is a variant of internal anatomy, when + * the organs are located in a mirror image. + * Something similar happened to our robot Kolli. His keys became values, + * and values became keys. Help to repair the robot. Create a 'inverseRobot' + * function that takes 'robot' as a parameter and returns a new object in which + * keys will change places with values. + * + * If any of the object values are repeated, return 'null'. + * + * Example: + * + * const kolli = { Kolli: 'name', 123: 'chipVer', 3: 'wheels' }; + * const robert = { Robert: 'name', 123: 'chipVer', 113: 'chipVer' }; + * inverseRobot(robert) === null + * inverseRobot(kolli) === { name: 'Kolli', chipVer: '123', wheels: '3' } + * + * * @param {object} robot * - * @return {object | null} -*/ - + * @return {object} + */ function inverseRobot(robot) { - const fixed = {}; - const valuesSet = new Set(); + let duplicate = Object.values(robot); + const obj = {}; - for (const key in robot) { - const value = robot[key]; + duplicate = duplicate.some((e, i, a) => a.indexOf(e) !== a.lastIndexOf(e)); - if (valuesSet.has(value)) { - return null; - } + if (duplicate) { + return null; + } else { + Object.keys(robot).forEach(e => { + obj[robot[e]] = e; + }); - fixed[value] = key; - valuesSet.add(value); + return obj; } - - return fixed; } module.exports = inverseRobot; From 6831bb9306d5f4732e1709e7eb7402859910e922 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Fri, 5 Apr 2024 12:06:48 +0300 Subject: [PATCH 03/18] solution 3 --- src/inverseRobot.js | 44 +++++++++++++------------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index cdc084d2..3c8e6c14 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -1,43 +1,25 @@ 'use strict'; -/** - * Situs inversus - * Transposition of internal organs is a variant of internal anatomy, when - * the organs are located in a mirror image. - * Something similar happened to our robot Kolli. His keys became values, - * and values became keys. Help to repair the robot. Create a 'inverseRobot' - * function that takes 'robot' as a parameter and returns a new object in which - * keys will change places with values. - * - * If any of the object values are repeated, return 'null'. - * - * Example: - * - * const kolli = { Kolli: 'name', 123: 'chipVer', 3: 'wheels' }; - * const robert = { Robert: 'name', 123: 'chipVer', 113: 'chipVer' }; - * inverseRobot(robert) === null - * inverseRobot(kolli) === { name: 'Kolli', chipVer: '123', wheels: '3' } - * - * +/* * @param {object} robot * * @return {object} - */ -function inverseRobot(robot) { - let duplicate = Object.values(robot); - const obj = {}; +*/ - duplicate = duplicate.some((e, i, a) => a.indexOf(e) !== a.lastIndexOf(e)); +function inverseRobot(robot) { + // write code here + const inversedRobot = {}; - if (duplicate) { - return null; - } else { - Object.keys(robot).forEach(e => { - obj[robot[e]] = e; - }); + for (const key in robot) { + const inversedKey = robot[key]; - return obj; + if (inversedRobot.hasOwnProperty(inversedKey)) { + return null; + } + inversedRobot[inversedKey] = key; } + + return inversedRobot; } module.exports = inverseRobot; From 0c9c947bb6edb63a488dec16db73ae86f9e351cb Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Fri, 5 Apr 2024 12:11:06 +0300 Subject: [PATCH 04/18] solution 4 --- src/inverseRobot.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 3c8e6c14..63162f55 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,16 +7,14 @@ */ function inverseRobot(robot) { - // write code here const inversedRobot = {}; for (const key in robot) { - const inversedKey = robot[key]; - - if (inversedRobot.hasOwnProperty(inversedKey)) { + if (!inversedRobot[robot[key]]) { + inversedRobot[robot[key]] = key; + } else { return null; } - inversedRobot[inversedKey] = key; } return inversedRobot; From b67f7e27ca04677b79b23b7e20c2cb6ef090de9f Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Fri, 5 Apr 2024 12:11:43 +0300 Subject: [PATCH 05/18] solution 5 --- src/inverseRobot.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 63162f55..3643bc51 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,17 +7,20 @@ */ function inverseRobot(robot) { - const inversedRobot = {}; + const KEYS = Object.values(robot); + const VALUES = Object.keys(robot); + const INVERSED_ROBOT = {}; + const UNIQUE_KEYS = [ ...new Set(KEYS) ]; - for (const key in robot) { - if (!inversedRobot[robot[key]]) { - inversedRobot[robot[key]] = key; - } else { - return null; - } + if (UNIQUE_KEYS.length !== VALUES.length) { + return null; } - return inversedRobot; + for (let i = 0; i < KEYS.length; i++) { + INVERSED_ROBOT[KEYS[i]] = VALUES[i]; + } + + return INVERSED_ROBOT; } module.exports = inverseRobot; From 66f7d93975b33f90397699770c8beaf6e4eb61f4 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 8 Apr 2024 20:18:43 +0300 Subject: [PATCH 06/18] solution 6 --- src/inverseRobot.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 3643bc51..d28fded0 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,20 +7,12 @@ */ function inverseRobot(robot) { - const KEYS = Object.values(robot); - const VALUES = Object.keys(robot); - const INVERSED_ROBOT = {}; - const UNIQUE_KEYS = [ ...new Set(KEYS) ]; + const newRobot = Object.fromEntries(Object.entries(robot) + .map(([key, value]) => + [value, key])); - if (UNIQUE_KEYS.length !== VALUES.length) { - return null; - } - - for (let i = 0; i < KEYS.length; i++) { - INVERSED_ROBOT[KEYS[i]] = VALUES[i]; - } - - return INVERSED_ROBOT; + return Object.keys(newRobot).length === Object.values(robot).length + ? newRobot : null; } module.exports = inverseRobot; From 412eb2433234ce0597685f4e16c468c5314c0430 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 8 Apr 2024 20:45:36 +0300 Subject: [PATCH 07/18] solution 7 --- src/inverseRobot.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index d28fded0..a81c5ecc 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,12 +7,20 @@ */ function inverseRobot(robot) { - const newRobot = Object.fromEntries(Object.entries(robot) - .map(([key, value]) => - [value, key])); + const REVERSE_ROBOT = {}; + const VALUES = Object.values(robot); + const KEYS = Object.keys(robot); + const REMOVE_DUBLICATE_VALUES = new Set(VALUES); - return Object.keys(newRobot).length === Object.values(robot).length - ? newRobot : null; + if ([...REMOVE_DUBLICATE_VALUES].length !== VALUES.length) { + return null; + } + + for (let i = 0; i < KEYS.length; i++) { + REVERSE_ROBOT[VALUES[i]] = KEYS[i]; + } + + return REVERSE_ROBOT; } module.exports = inverseRobot; From 2c949bd1f85586ffaff2bb284b80dde733b3812b Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 8 Apr 2024 21:03:29 +0300 Subject: [PATCH 08/18] solution 8 --- src/inverseRobot.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index a81c5ecc..e551cbb8 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,20 +7,28 @@ */ function inverseRobot(robot) { - const REVERSE_ROBOT = {}; - const VALUES = Object.values(robot); - const KEYS = Object.keys(robot); - const REMOVE_DUBLICATE_VALUES = new Set(VALUES); + // write code here + let tempKyesRobot = {}; + let tempValueRobot = {}; - if ([...REMOVE_DUBLICATE_VALUES].length !== VALUES.length) { - return null; + tempKyesRobot = Object.keys(robot); + tempValueRobot = Object.values(robot); + + for (let i = 0; i < tempValueRobot.length; i++) { + if (tempValueRobot.indexOf(tempValueRobot[i]) !== i) { + return null; + } + } + + for (const key in robot) { + delete robot[key]; } - for (let i = 0; i < KEYS.length; i++) { - REVERSE_ROBOT[VALUES[i]] = KEYS[i]; + for (const i in tempKyesRobot) { + robot[tempValueRobot[i]] = tempKyesRobot[i]; } - return REVERSE_ROBOT; + return robot; } module.exports = inverseRobot; From 07d32d3079edf754b6cea8ae640be1e4f5710789 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 8 Apr 2024 21:26:13 +0300 Subject: [PATCH 09/18] solution 9 --- src/inverseRobot.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index e551cbb8..98c31ac6 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,28 +7,25 @@ */ function inverseRobot(robot) { - // write code here - let tempKyesRobot = {}; - let tempValueRobot = {}; + const inverse = {}; + const propertyValues = Object.values(robot); + let current; - tempKyesRobot = Object.keys(robot); - tempValueRobot = Object.values(robot); + for (let i = 0; i < propertyValues.length; i++) { + current = propertyValues[i]; - for (let i = 0; i < tempValueRobot.length; i++) { - if (tempValueRobot.indexOf(tempValueRobot[i]) !== i) { - return null; + for (let j = i + 1; j < propertyValues.length; j++) { + if (current === propertyValues[j]) { + return null; + } } } for (const key in robot) { - delete robot[key]; + inverse[robot[key]] = key; } - for (const i in tempKyesRobot) { - robot[tempValueRobot[i]] = tempKyesRobot[i]; - } - - return robot; + return inverse; } module.exports = inverseRobot; From 727503c5da08ee71f57c6c772fcd2dc2f769e8a4 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 8 Apr 2024 21:31:56 +0300 Subject: [PATCH 10/18] solution 10 --- src/inverseRobot.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 98c31ac6..5dac61e2 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,25 +7,17 @@ */ function inverseRobot(robot) { + // write code here const inverse = {}; - const propertyValues = Object.values(robot); - let current; - for (let i = 0; i < propertyValues.length; i++) { - current = propertyValues[i]; - - for (let j = i + 1; j < propertyValues.length; j++) { - if (current === propertyValues[j]) { - return null; - } + for (const key in robot) { + if (Object.prototype.hasOwnProperty.call(inverse, robot[key])) { + return null; } - } - for (const key in robot) { inverse[robot[key]] = key; } return inverse; } - module.exports = inverseRobot; From a44140ddc05794b2932d44b764ed7dfa5bb78dcb Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Tue, 9 Apr 2024 17:20:56 +0300 Subject: [PATCH 11/18] solution 9.2 --- src/inverseRobot.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 5dac61e2..98c31ac6 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,17 +7,25 @@ */ function inverseRobot(robot) { - // write code here const inverse = {}; + const propertyValues = Object.values(robot); + let current; - for (const key in robot) { - if (Object.prototype.hasOwnProperty.call(inverse, robot[key])) { - return null; + for (let i = 0; i < propertyValues.length; i++) { + current = propertyValues[i]; + + for (let j = i + 1; j < propertyValues.length; j++) { + if (current === propertyValues[j]) { + return null; + } } + } + for (const key in robot) { inverse[robot[key]] = key; } return inverse; } + module.exports = inverseRobot; From 6320c61fe6763bc4a99a18198240637c19cd5a3b Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Tue, 9 Apr 2024 19:24:27 +0300 Subject: [PATCH 12/18] solution 8.2 --- src/inverseRobot.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 98c31ac6..e551cbb8 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,25 +7,28 @@ */ function inverseRobot(robot) { - const inverse = {}; - const propertyValues = Object.values(robot); - let current; + // write code here + let tempKyesRobot = {}; + let tempValueRobot = {}; - for (let i = 0; i < propertyValues.length; i++) { - current = propertyValues[i]; + tempKyesRobot = Object.keys(robot); + tempValueRobot = Object.values(robot); - for (let j = i + 1; j < propertyValues.length; j++) { - if (current === propertyValues[j]) { - return null; - } + for (let i = 0; i < tempValueRobot.length; i++) { + if (tempValueRobot.indexOf(tempValueRobot[i]) !== i) { + return null; } } for (const key in robot) { - inverse[robot[key]] = key; + delete robot[key]; } - return inverse; + for (const i in tempKyesRobot) { + robot[tempValueRobot[i]] = tempKyesRobot[i]; + } + + return robot; } module.exports = inverseRobot; From b589d37ebaec9070bd3f019c9b92e395013e3823 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 16:23:01 +0300 Subject: [PATCH 13/18] top solution --- src/inverseRobot.js | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index e551cbb8..bdb432ee 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,28 +7,16 @@ */ function inverseRobot(robot) { - // write code here - let tempKyesRobot = {}; - let tempValueRobot = {}; + const invertedRobot = {}; - tempKyesRobot = Object.keys(robot); - tempValueRobot = Object.values(robot); - - for (let i = 0; i < tempValueRobot.length; i++) { - if (tempValueRobot.indexOf(tempValueRobot[i]) !== i) { + for (const key in robot) { + if (invertedRobot[robot[key]]) { return null; } + invertedRobot[robot[key]] = key; } - for (const key in robot) { - delete robot[key]; - } - - for (const i in tempKyesRobot) { - robot[tempValueRobot[i]] = tempKyesRobot[i]; - } - - return robot; + return invertedRobot; } module.exports = inverseRobot; From 408ab78126728c150b359d59e2b52e48e969b21f Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 16:33:36 +0300 Subject: [PATCH 14/18] top solution fixed --- src/inverseRobot.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index bdb432ee..408830c4 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -10,10 +10,11 @@ function inverseRobot(robot) { const invertedRobot = {}; for (const key in robot) { - if (invertedRobot[robot[key]]) { + if (invertedRobot.hasOwnProperty(robot[key])) { return null; } - invertedRobot[robot[key]] = key; + + invertedRobot[`${robot[key]}`] = key; } return invertedRobot; From fc864ed136b4a3d1913553b6c621d40708521979 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 16:35:57 +0300 Subject: [PATCH 15/18] top solution fixed 2 --- src/inverseRobot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 408830c4..80d21c39 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -14,7 +14,7 @@ function inverseRobot(robot) { return null; } - invertedRobot[`${robot[key]}`] = key; + invertedRobot[String(robot[key])] = key; } return invertedRobot; From 297f3ed07e6a9728f77e1347224b2ae374ea2916 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 16:39:16 +0300 Subject: [PATCH 16/18] top solution fixed 3 --- src/inverseRobot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 80d21c39..a2ac3e92 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -10,7 +10,7 @@ function inverseRobot(robot) { const invertedRobot = {}; for (const key in robot) { - if (invertedRobot.hasOwnProperty(robot[key])) { + if (Object.prototype.hasOwnProperty.call(invertedRobot, robot[key])) { return null; } From c1b56092dfdff87c4532fd6a128fe1532ce62783 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 17:31:36 +0300 Subject: [PATCH 17/18] top solution fixed 4 --- src/inverseRobot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index a2ac3e92..4b52c61f 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -10,11 +10,11 @@ function inverseRobot(robot) { const invertedRobot = {}; for (const key in robot) { - if (Object.prototype.hasOwnProperty.call(invertedRobot, robot[key])) { + if (robot[key] in invertedRobot) { return null; } - invertedRobot[String(robot[key])] = key; + invertedRobot[robot[key]] = key; } return invertedRobot; From 0d59f0691eb0c6e2c7e27e4e4c56111f8ef9c264 Mon Sep 17 00:00:00 2001 From: Volodymyr Kryvytskyi Date: Mon, 15 Apr 2024 20:21:21 +0300 Subject: [PATCH 18/18] new solution --- src/inverseRobot.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/inverseRobot.js b/src/inverseRobot.js index 4b52c61f..e1bb068f 100644 --- a/src/inverseRobot.js +++ b/src/inverseRobot.js @@ -7,17 +7,18 @@ */ function inverseRobot(robot) { - const invertedRobot = {}; + const clone = {}; for (const key in robot) { - if (robot[key] in invertedRobot) { + const newKey = robot[key]; + + if (clone.hasOwnProperty(newKey)) { return null; } - - invertedRobot[robot[key]] = key; + clone[newKey] = key; } - return invertedRobot; + return clone; } module.exports = inverseRobot;