Last active
October 7, 2022 04:37
-
-
Save OlegKorn/d151f51f2c51ed832bd713f989aafec6 to your computer and use it in GitHub Desktop.
JS: finding a value by a key in dictionary
This file contains hidden or 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
function isThereEquivalent(equivalentsArr, value) { | |
try { | |
var equivalent = Object.entries(equivalentsArr).find(([key]) => key === value.toString())[1] | |
return equivalent | |
} | |
catch (e) { | |
if (e instanceof TypeError) { | |
return false | |
} | |
} | |
} | |
/* | |
EXAMPLE | |
var equivalents = { | |
1: "I", | |
4: "IV", | |
5: "V", | |
9: "IX", | |
10: "X", | |
40: "XL", | |
50: "L", | |
90: "XC", | |
100: "C", | |
400: "CD", | |
500: "D", | |
900: "CM", | |
1000: "M" | |
} | |
isThereEquivalent(equivalents, 90) returns XC | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment