-
Notifications
You must be signed in to change notification settings - Fork 0
/
findKey.js
51 lines (43 loc) · 1.22 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
43
44
45
46
47
48
49
50
51
const assertEqual =(actual, expected) => {
if (actual === expected) {
console.log(`✅✅✅ Assertation Passed: ${actual} === ${expected}`);
} else {
console.log(`❗️❗️❗️ Assertation Failed: ${actual} !== ${expected}`);
}
};
//Cleaned Up
const findKey = (object, callback) => {
for (let index of Object.keys(object)) {
if (callback (object[index]) === true) {
console.log(index);
return index;
}
}
};
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") // => "noma"
// callback
//Notes
//function findKey (object, callback)
//scan and return first key that returns a truthy value
//otherwise return undefined
//First Draft
// const findKey = function (object, callback) {
// // let keyArray = Object.keys(object);
// //console.log(`Test: ${keyArray}`);
// //for each index of key array, if that key value.
// //console.log(object[i]);
// for (let i of Object.keys(object)) {
// if (callback(object[i]) === true) {
// console.log(i);
// return i;
// }
// // }
// }
// }