Advertisement
Kamend1

06.Word tracker

Mar 12th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wordsTracker(array) {
  2.     let searchedWords = array[0].split(' ');
  3.     let targetWords = array.splice(1, array.length - 1);
  4.     let wordObj = {};
  5.  
  6.     for (let word of searchedWords) {
  7.         wordObj[word] = 0;
  8.  
  9.         for (let element of targetWords) {
  10.             if (element === word) {
  11.                 wordObj[word] += 1;
  12.             }
  13.         }
  14.     }
  15.  
  16.     let sortedEntries = Object.entries(wordObj).sort((a, b) => b[1] - a[1])
  17.  
  18.     for (let entry of sortedEntries) {
  19.         console.log(`${entry[0]} - ${entry[1]}`)
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement