Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wordsTracker(arr) {
- const wordsToFind = arr.shift().split(' ');
- const wordCounts = {};
- arr.forEach(sentence => {
- wordsToFind.forEach(word => {
- const wordsInSentence = sentence.split(' ');
- const count = wordsInSentence.reduce((acc, curr) => {
- if (curr === word) {
- return acc + 1;
- }
- return acc;
- }, 0);
- wordCounts[word] = (wordCounts[word] || 0) + count;
- });
- });
- const sortedCounts = Object.entries(wordCounts)
- .sort((a, b) => b[1] - a[1]);
- const output = sortedCounts
- .map(([word, count]) => `${word} - ${count}`)
- .join('\n');
- console.log(output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement