-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindKey.js
43 lines (38 loc) · 1.09 KB
/
findKey.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
// FUNCTION IMPLEMENTATION
// returns the key of the first element predicate returns truthy for
const findKey = function(object, callback) {
for (let key in object) {
if (callback(object[key])) {
return key;
}
}
return 'No Key with this value'
}
module.exports = findKey;
/* Test Code
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: '${actual}' === '${expected}'`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: '${actual}' !== '${expected}'`);
}
};
test1 = findKey({
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}, x => x.stars === 2) // => "noma"
assertEqual(test1, 'noma');
test2 = findKey({
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}, x => x.stars === 4) // => "No Key with this value"
assertEqual(test2, 'No Key with this value');
*/