-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountOnly.js
35 lines (32 loc) · 1.05 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
//psueodcode
// 1. search through itemsToCount to see what keys we need.
// 2. if itemsToCount.value === true, and itemsToCount === allItems;
// 3. push key-value pair
/** Function: contOnly() takes an array and object and returns counts of everything th object listed.
*
* @param {Array} allItems - contains strings the function searches through
* @param {Array} itemsToCount - specifying what to count
*
* @returns {Object} - Object that contains the number of times a specified keys occurs
*
**/
const countOnly = function(allItems, itemsToCount) {
let countResult = {};
for (let item in itemsToCount) {
if (itemsToCount[item]) {
// if true, then count occurences in allItems
for (let key of allItems) {
// loop through allItems to count each value
if (key === item) {
// if the string in allItems is in itemsToCount, count it
if (!countResult[item]) {
countResult[item] = 0;
}
countResult[item] += 1;
}
}
}
}
return countResult;
};
module.exports = countOnly;