SHARE
TWEET

01. Word Tracker Associative Arrays

Lulunga Jul 11th, 2019 63 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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top