function countWords(inputWords) {
var sortArray = inputWords.sort(function (idx1, idx2) { return idx1.localeCompare(idx2) });
var resultArray = [];
var counter = 1;
sortArray.reduce(function (total, currentValue) {
if (total == currentValue)
counter++;
else {
resultArray.push(counter);
counter = 1;
}
return currentValue;
});
return resultArray;
}
module.exports = countWords
So currently I'm trying to get the number of times each string occurs in an Array. I wrote the function above, but it seems that it has a bug.
PS: That is an exercise of https://nodeschool.io/ .. npm install -g functional-javascript-workshop . I have to use the reduce function and I'm not allowed to use any for/while loops
That was my input/output:
input: [ 'ad', 'ad', 'ad', 'aliqua', 'aliquip', 'aute', 'consectetur', 'consequat', 'culpa', 'culpa', 'culpa', 'do', 'do', 'dolor', 'dolore', 'eiusmod', 'elit', 'esse', 'esse', 'est', 'et', 'et', 'ex', 'excepteur', 'excepteur', 'exercitation', 'exercitation', 'id', 'id', 'id', 'incididunt', 'labore', 'laborum', 'lorem', 'magna', 'minim', 'minim', 'minim', 'mollit', 'nostrud', 'nulla', 'pariatur', 'proident', 'qui', 'qui', 'reprehenderit', 'reprehenderit', 'sint', 'ullamco', 'velit' ]
submission: [ 3, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 2, 2, 1, 1 ]
The workshop "says" that this is wrong.
Can somebody help?