-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKey.js
42 lines (37 loc) · 1.02 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
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`Assertion passed: ${actual} === ${expected}`);
} else {
console.log(`Assertion failed: ${actual} !== ${expected}`);
}
};
const findKey = function (object, callback) {
let keysArr = Object.keys(object);
for (let i = 0; i < keysArr.length; i++) {
if (callback(object[keysArr[i]])) {
return keysArr[i];
}
}
return undefined;
};
findKey({
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}, x => x.stars === 2);
let object = {
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
};
let actual1 = findKey(object, x => x.stars === 2);
let expected1 = "noma";
assertEqual(actual1, expected1);
assertEqual(actual1, expected1);
module.exports = findKey;