@kowo0403hk/lotide
Version:
lotide library by LHL Bootcamp student
43 lines (37 loc) • 1.29 kB
JavaScript
// 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 assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`\u2705\u2705\u2705 Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`\u26d4\u26d4\u26d4 Assertion Failed: ${actual} !== ${expected}`);
}
};
const findKey = (obj, callback) => {
for (let key in obj) {
if (callback(obj[key])) return key;
}
return undefined;
};
// test case
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"
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");
assertEqual(findKey({
"Surrey": { expLevel: 3},
"Delta": { expLevel: 5},
"Vancouver": { expLevel: 9},
"Burnaby": { expLevel: 7},
}, x => x.expLevel === 9), "Vancouver");