Advertisement
Lulunga

01. Word Tracker Associative Arrays

Jul 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function store(input) {
  2.     let words = input
  3.         .shift()
  4.         .split(' ');
  5.     let wordsCount = {};
  6.     for (let word of words) {
  7.         wordsCount[word] = 0;
  8.     }
  9.     for (let i = 0; i < input.length; i++) {
  10.         if (wordsCount.hasOwnProperty(input[i])) {
  11.             wordsCount[input[i]]++;
  12.         }
  13.  
  14.     }
  15.     let sorted = Object.entries(wordsCount).sort((firstPair, secondPair) => {
  16.         let [firstWord, firstOccurence] = firstPair;
  17.         let [secondWord, secondOccurence] = secondPair;
  18.         return secondOccurence - firstOccurence;
  19.     });
  20.     sorted.forEach(element => {
  21.         console.log(`${element.join(' - ')}`);
  22.     });
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement