Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Менять код ниже этого комментария.
  2. /**
  3.  *
  4.  * @param {Array.<String>} wordsArr
  5.  */
  6. function unifier(wordsArr) {
  7.     /*The function 'unifier' calculates the number of occurences of each word in an array and stores the value in an result object property (resobj) with the same name.*/
  8.     /**
  9.      *
  10.      * @param {String} word
  11.      * @param {Array.<Object>} arr
  12.      * @returns {Number|Boolean} returns index of word in array or false
  13.      */
  14.    
  15.      let isWordExists = (word, arr) => {
  16.         for (let i = 0; i < arr.length; i++) {
  17.             if (arr[i].word == word) {
  18.                 return i;
  19.             }
  20.         }
  21.         return false;
  22.     }
  23.  
  24.     let resObj = { wordRepeats: [] };
  25.    
  26.     wordsArr.forEach(word => {
  27.         let wordIndex = isWordExists(word, resObj.wordRepeats);
  28.         if (wordIndex) {
  29.             //@ts-ignore
  30.             resObj.wordRepeats[wordIndex].itr++;
  31.         } else {
  32.             resObj.wordRepeats.push({word: word, itr: 1});
  33.         }
  34.     })
  35.  
  36.     return resObj;
  37. }
  38. //Менять код выше этого комментария.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement