-
Notifications
You must be signed in to change notification settings - Fork 0
/
countOnly.js
45 lines (39 loc) · 1 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
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✔✔✔Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`⛔⛔⛔Assertion Failed: ${actual} !== ${expected}`);
}
};
const countOnly = function(allItems, itemsToCount) {
const results = {};
for (const item of allItems) {
console.log(item);
if (itemsToCount[item]) {
if (results[item]) {
results[item] += 1;
} else {
results[item] = 1;
}
}
}
return results;
};
// Test Cases
const firstNames = [
"Adam",
"Mog",
"Lightning",
"Fang",
"Kavith",
"Jason",
"Salima",
"Fang",
"Adam"
];
// itemstoCount == firstnames elements from array and checking to see if they are truthy?
const result1 = countOnly(firstNames, { "Adam": true, "Bob": true, "Fang": true, "Lightning": false,});
assertEqual(result1["Adam"], 2);
assertEqual(result1["Bob"], undefined);
assertEqual(result1["Fang"], 4);
assertEqual(result1["Lightning"], undefined);