-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKey.js
40 lines (35 loc) · 822 Bytes
/
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
//Dependencies
const assertEqual = require('./assertEqual').default;
const findKey = (obj, callback) => {
for(let key in obj){
if(callback(obj[key])){
return key;
}
}
};
module.exports = findKey;
//Test Code
// assertEqual(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'); // => "noma"
// assertEqual(findKey({
// a: 'hustlin',
// b: 'hustlin',
// c: 'hustlin',
// d: 'money',
// e: 'money'
// }, x => x === 'money'), 'd');
// assertEqual(findKey({
// cowards: 3974,
// come: 32097,
// here: 4892,
// no: 3229,
// more: 8573,
// foolish: 232,
// one: 1254,
// }, x => (x%4===0)), 'here');