Skip to content

Commit

Permalink
solution 3
Browse files Browse the repository at this point in the history
  • Loading branch information
vkryvytskyy committed Apr 5, 2024
1 parent a946d53 commit 6831bb9
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions src/inverseRobot.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 6831bb9

Please sign in to comment.