Todorov_Stanimir

01. Word Tracker with Map

Jul 10th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wordTracker(input) {
  2.     let words = new Map();
  3.     input.shift()
  4.         .split(' ')
  5.         .forEach(word => {
  6.             words.set(word, input.slice(0).filter(chekingWord => chekingWord === word).length)
  7.         });
  8.  
  9.     [...words]
  10.         .sort((a, b) => b[1] - a[1])
  11.         .forEach(element => console.log(`${element[0]} - ${element[1]}`));
  12. }
  13. wordTracker([
  14.     'this sentence', 'In', 'this', 'sentence', 'you', 'have', 'to', 'count', 'the', 'occurances', 'of', 'the'
  15.     , 'words', 'this', 'and', 'sentence', 'because', 'this', 'is', 'your', 'task'
  16. ])
Advertisement
Add Comment
Please, Sign In to add comment