-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindKey.js
More file actions
45 lines (35 loc) · 1.41 KB
/
findKey.js
File metadata and controls
45 lines (35 loc) · 1.41 KB
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
43
44
45
const { callbackify } = require("util");
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
//findKey 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, it should return undefined.
const findKey = function(object, callback) {
const results = Object.keys(object); //store keys of object (movie names) in an array called results
for (let key of results) { //for each movie within the array of movie names
if (callback(object[key])) { //compares callback.stars value with the key-value pair (in this case, the stars for each movie) and exits after it finds the first match
return key;
}
}
}
module.exports = findKey;
console.log(findKey({
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}, callback => callback.stars === 2));
let result = findKey({
"Blue Hill": { stars: 1 },
"Akaleri": { stars: 3 },
"noma": { stars: 2 },
"elBulli": { stars: 3 },
"Ora": { stars: 2 },
"Akelarre": { stars: 3 }
}, callback => callback.stars === 2);
assertEqual(result, "noma");