/
countOnly.js
46 lines (39 loc) · 1.43 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
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
//only keys that has truthy value should be counted in the resulting object. all other strings are either set to false orn ot included at all in the object should not be counted.
//setting up a function that is caled countOnly and takes 2 parameters and it should report back how many instances of each string were found in the allItems array of strings. so allItems is an arrat of strings;
//countOnly is a function that would see how many strings found in the iput array and their respective counts. so it will return an object to represent how many strings have a truthy value in the object.
const countOnly = function (allItems, itemsToCount) {
const results = {}
for (const item of allItems) {
// console.log(item)
if (results[item]) {
if (results[item]) {
results[item] += 1;
} else {
results[item] = 1;
}
}
}
return results;
}
const firstNames = [
"Karl",
"Salima",
"Agouhanna",
"Fang",
"Kavith",
"Jason",
"Salima",
"Fang",
"Joe"
];
const result1 = countOnly(firstNames, { "Jason": true, "Karima": true, "Fang": true });
assertEqual(result1["Jason"], 1);
assertEqual(result1["Karima"], undefined);
assertEqual(result1["Fang"], 2);