-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKey.js
30 lines (26 loc) · 907 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
//assertEqual function
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`Assertion Passed: ${actual} ✅✅✅ ${expected}`);
} else {
console.log(`Assertion Failed: ${actual} 🛑🛑🛑 ${expected}`);
}
};
//Implement the function findKey which takes in an object and a callback. It should scan the object and return the first key for which the callback returns a truthy value. If no key is found, then it should return undefined.
const findKey = function(object, callback) {
for (let key in object) {
if (callback(object[key]) === true) {
return key;
}
}
};
module.exports = findKey;
//Test code
console.log(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"