-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKeyByValue.js
56 lines (46 loc) · 1.49 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//ASSERTION FUNCTION
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`💚 Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🔴 Assertion Failed: ${actual} !== ${expected}`);
}
};
//ACTUAL FUNCTION
const findKeyByValue = function (object, value) {
let keys = Object.keys(object);
for (let key of keys) {
if (object[key] === value) {
return key;
}
}
}
module.exports = findKeyByValue;
//TEST CASE 1
// const bestTVShowsByGenre = {
// sci_fi: "The Expanse",
// comedy: "Brooklyn Nine-Nine",
// drama: "The Wire"
// };
// assertEqual(findKeyByValue(bestTVShowsByGenre, "The Wire"), "drama");// => should PASS
// assertEqual(findKeyByValue(bestTVShowsByGenre, "That '70s Show"), undefined);// => should PASS
// //TEST CASE 2
// const actualBestTVShowsByGenre = {
// comedy: "Seinfeld",
// drama: "The Sopranos",
// crime: "Peaky Blinders"
// };
// assertEqual(findKeyByValue(actualBestTVShowsByGenre, "The Sopranos"), "drama");// => should PASS
// assertEqual(findKeyByValue(bestTVShowsByGenre, "Breaking Bad"), undefined);// => should PASS
// //TEST CASE 3
// console.log(findKeyByValue({
// comedy: "Seinfeld",
// drama: "The Sopranos",
// crime: "Peaky Blinders"
// }, "Seinfeld")); // => should return 'comedy'
// //TEST CASE 4
// console.log(findKeyByValue({
// comedy: "Seinfeld",
// drama: "The Sopranos",
// crime: "Peaky Blinders"
// }, "Breaking Bad")); // => should return undefined