-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountOnly.js
66 lines (53 loc) · 1.76 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
This function should take in a collection of items
and return counts for a specific subset of those items.
It won't count everything.
In order to decide what to count, it will also be given
an idea of which items we care about and it will only count those,
ignoring the others.
Items in our case will be limited to Strings.
*/
const assertArraysEqual = require('./assertArraysEqual');
const assertEqual = require('./assertEqual');
/////// ^^CALLING PREVIOUS FUNCTION^^ /////////////
// allItems: an array of strings that we need to look through
// itemsToCount: an object specifying what to count
const countOnly = function(allItems, itemsToCount) {
const results = {};
for (const item of allItems) {
// console.log(item);
// inside the loop,increment the counter for each item:
// set a property with that string key to:
// the value that was already there (or zero if nothing there) with 1 added to it.
// inside the loop,increment the counter for each item:
// inside the loop:
// console.log("itemstocount: ",itemsToCount[item]);
if (itemsToCount[item]) {
if (results[item]) {
// console.log("RESULTS ITEM:", results[item])
results[item] += 1;
} else {
results[item] = 1;
}
}
}
return results;
}
module.exports = countOnly;
/// TEST CONDITIONS ///////
// 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);