Advertisement
nikolayneykov

Untitled

Apr 13th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.     let videos = {};
  3.     let sortingCriteria = params.pop().split(' ')[1];
  4.     params.pop();
  5.  
  6.     for (let param of params) {
  7.         if (param.includes('-')) {
  8.             let [videoName, viewsCount] = param.split('-');
  9.  
  10.             if (!videos.hasOwnProperty(videoName)) {
  11.                 videos[videoName] = { views: 0, likes: 0 };
  12.             }
  13.  
  14.             videos[videoName].views += Number(viewsCount);
  15.         } else {
  16.             let [rate, videoName] = param.split(':');
  17.  
  18.             if (videos.hasOwnProperty(videoName)) {
  19.                 videos[videoName].likes += rate === 'like' ? 1 : -1;
  20.             }
  21.         }
  22.     }
  23.  
  24.     Object.entries(videos)
  25.         .sort((a,b)=>b[1][sortingCriteria]-a[1][sortingCriteria])
  26.         .forEach(v=>console.log(`${v[0]} - ${v[1].views} views - ${v[1].likes} likes`));
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement