-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKeyByValue.js
31 lines (28 loc) · 1.03 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
// Function to compare values and print assertions
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`Assertion passed: ${actual} === ${expected}`);
} else {
console.log(`Assertion Failed: ${actual} !== ${expected}`);
}
};
// Function to find a key by its corresponding value in an object
const findKeyByValue = function(object, value) {
for (const key of Object.keys(object)) {
if (object[key] === value) {
return key;
}
}
return undefined;
};
// Test cases
const bestTVShowsByGenre = {
sci_fi: "The Expanse",
comedy: "Brooklyn Nine-Nine",
drama: "The Wire"
};
assertEqual(findKeyByValue(bestTVShowsByGenre, "The Wire"), "drama");
assertEqual(findKeyByValue(bestTVShowsByGenre, "Brooklyn Nine-Nine"), "comedy");
assertEqual(findKeyByValue(bestTVShowsByGenre, "That '70s Show"), undefined);
assertEqual(findKeyByValue({}, "Test"), undefined);
assertEqual(findKeyByValue(bestTVShowsByGenre, "The Expanse"), "sci_fi");