-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabyNames.js
96 lines (85 loc) · 2.15 KB
/
babyNames.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var babyNames = function () {
var names = [
{
"rank": "1",
"name": "Noah",
"sex": "boy"
},
{
"rank": "1",
"name": "Emma",
"sex": "girl"
},
{
"rank": "2",
"name": "Liam",
"sex": "boy"
},
{
"rank": "2",
"name": "Olivia",
"sex": "girl"
},
{
"rank": "3",
"name": "Ethan",
"sex": "boy"
},
{
"rank": "3",
"name": "Ava",
"sex": "girl"
},
{
"rank": "4",
"name": "Lucas",
"sex": "boy"
},
{
"rank": "4",
"name": "Sophia",
"sex": "girl"
},
{
"rank": "5",
"name": "Mason",
"sex": "boy"
},
{
"rank": "5",
"name": "Isabella",
"sex": "girl"
}
];
function getBoyName(rank) {
//iterate through names and return all names
if (rank == undefined || rank == "all")
return (grep(names, function (p) { return p.sex == 'boy'; }).map(function (p) { return p.name; }));
return grep(names, function (p) { return p.sex == 'boy' && p.rank == rank; })
.map(function (p) { return p.name; });
}
function getGirlName(rank) {
if (rank == undefined || rank == "all")
return (grep(names, function (p) { return p.sex == 'girl'; }).map(function (p) { return p.name; }));
return grep(names, function (p) { return p.sex == 'girl' && p.rank == rank; })
.map(function (p) { return p.name; });;
}
return {
getBoyName: getBoyName,
getGirlName: getGirlName
}
};
module.exports = babyNames;
var grep = function (items, callback) {
var filtered = [],
len = items.length,
i = 0;
for (i; i < len; i++) {
var item = items[i];
var cond = callback(item);
if (cond) {
filtered.push(item);
}
}
return filtered;
};