-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a946d53
commit 6831bb9
Showing
1 changed file
with
13 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |