-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKey.js
32 lines (30 loc) · 873 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
// takes in an object and callback function
// scans object and return the first key for which the callback returns a truthy value
// if no key found, it should return undefined;
const findKey = function(object, callback) {
for (const property in object) {
let value = object[property];
if (callback(value)) {
return property;
}
}
};
module.exports = findKey;
/*
const assertEqual = require('./assertEqual');
// TEST CASE 1
const object1 = {
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
};
assertEqual(findKey(object1, x => x.stars === 2), "noma");
// TEST CASE 2
const object2 = { "a": 1, "b": 2, "c": 3 };
assertEqual(findKey(object2, x => x === 1), "a");
// TEST CASE 3
assertEqual(findKey(object1, x => x.stars === 4), undefined);
*/