-
Notifications
You must be signed in to change notification settings - Fork 0
/
countOnly.js
48 lines (43 loc) · 1.28 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
// 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 (let i = 0; i < allItems.length; i++) {
for (const item in itemsToCount) {
if (itemsToCount[item] && allItems[i] === item) {
if (results[item]) { // if the item keyword already has a value (1, 2 = true value), if not (0 = falsey value)
results[item] += 1;
} else {
results[item] = 1;
// = create a keyword and assign the value of 1 at the same time
// for the first time in the results object
}
}
}
}
return results;
}
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`assertion passed`);
} else {
console.log(`🛑 ${actual} !== ${expected}`);
}
};
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);
module.exports = countOnly;