-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountOnly.js
43 lines (36 loc) · 965 Bytes
/
countOnly.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
// Import assertEqual
const assertEqual = require('./assertEqual');
//Takes in a collection of items and returns a specific subset of those items
const countOnly = function(allItems, itemsToCount) {
let newObject = {};
for (const items of allItems) {
for (const key in itemsToCount) {
if (items === key && itemsToCount[key]) {
if (newObject.hasOwnProperty(items) === true) {
newObject[items] += 1;
} else {
newObject[items] = 1;
}
}
}
}
return newObject;
};
module.exports = countOnly;
// TEST CODE
const firstNames = [
"Karl",
"Salima",
"Agouhanna",
"Fang",
"Kavith",
"Jason",
"Salima",
"Fang",
"Joe"
];
const result1 = countOnly(firstNames, { "Jason": true, "Karima": true, "Fang": true, "Agouhanna": false });
assertEqual(result1["Jason"], 1);
assertEqual(result1["Karima"], undefined);
assertEqual(result1["Fang"], 2);
assertEqual(result1["Agouhanna"], undefined);