-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKeyByValue.js
36 lines (30 loc) · 939 Bytes
/
findKeyByValue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// FUNCTION IMPLEMENTATION
const assertEqual = function(actual, expected) {
// values matching
if (actual === expected) {
console.log(`😊😊😊 Assertion Passed: ${actual} === ${expected}`);
} else {
// Values not matching
console.log(`😡😡😡 Assertion Failed: ${actual} !== ${expected}`);
}
};
//Implement function
const findKeyByValue = function(inputObject, inputValue) {
// convert object to key's array
const objKeysArr = Object.keys(inputObject);
// iterate over object
for (let valtoSearch of objKeysArr) {
if (inputObject[valtoSearch] === inputValue) {
return `${valtoSearch}`;
}
}
return undefined;
};
const bestTVShowsByGenre = {
sci_fi: "The Expanse",
comedy: "Brooklyn Nine-Nine",
drama: "The Wire"
};
//Tests
assertEqual(findKeyByValue(bestTVShowsByGenre, "The Wire"), "drama");
assertEqual(findKeyByValue(bestTVShowsByGenre, "That '70s Show"), undefined);