forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtally.js
More file actions
36 lines (30 loc) · 770 Bytes
/
tally.js
File metadata and controls
36 lines (30 loc) · 770 Bytes
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
function tally(inputArray) {
if (Array.isArray(inputArray)) {
if (inputArray.length === 0) {
return {};
}
let itemCount = 0;
const tallyObject = {};
let n = 0;
while (inputArray.length > 0) {
const tempArray = [];
let i = 0;
let currentArrayItem = inputArray[0];
while (i < inputArray.length) {
if (currentArrayItem === inputArray[i]) {
itemCount++;
tempArray.push(inputArray.splice(i, 1));
i--;
}
i++;
}
if (tempArray.length > 0) {
tallyObject[tempArray[0]] = tempArray.length;
}
}
return tallyObject;
} else {
throw new Error("error invalid input passed, please provide an array");
}
}
module.exports = tally;