-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKeyByValue.js
37 lines (31 loc) · 1 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
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`😂😂😂 Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`😈😈😈 Assertion Failed: ${actual} !== ${expected}`);
}
};
const findKeyByValue = function (object, value) { // converting the object to an array
let objectArray = Object.keys(object)
// console.log(objectArray)
for (const key of objectArray){
if(object[key]===value){
return key;
}
}
// console.log key);
// if (objectArray[] === value) {
// console.log(value);
// console.log(objectArray key])
// }
}
//test cases
const bestTVShowsByGenre = {
sci_fi: "The Expanse",
comedy: "Brooklyn Nine-Nine",
drama: "The Wire"
};
assertEqual(findKeyByValue(bestTVShowsByGenre, "The Wire"), "drama");
assertEqual(findKeyByValue(bestTVShowsByGenre, "That '70s Show"), undefined);
assertEqual(findKeyByValue(bestTVShowsByGenre, "The Expanse"), "sci_fi");
module.exports = findKeyByValue;