Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Менять код ниже этого комментария.
- /**
- *
- * @param {Array.<String>} wordsArr
- */
- function unifier(wordsArr) {
- /*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.*/
- /**
- *
- * @param {String} word
- * @param {Array.<Object>} arr
- * @returns {Number|Boolean} returns index of word in array or false
- */
- let isWordExists = (word, arr) => {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i].word == word) {
- return i;
- }
- }
- return false;
- }
- let resObj = { wordRepeats: [] };
- wordsArr.forEach(word => {
- let wordIndex = isWordExists(word, resObj.wordRepeats);
- if (wordIndex) {
- //@ts-ignore
- resObj.wordRepeats[wordIndex].itr++;
- } else {
- resObj.wordRepeats.push({word: word, itr: 1});
- }
- })
- return resObj;
- }
- //Менять код выше этого комментария.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement