Advertisement
kstoyanov

07. ** Monkey Patcher

Oct 9th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function solution() {
  2.   function call(post, command) {
  3.     if (command === 'upvote') {
  4.       post.upvotes++;
  5.     } else if (command === 'downvote') {
  6.       post.downvotes++;
  7.     } else if (command === 'score') {
  8.       const output = [];
  9.       const totalPost = post.upvotes + post.downvotes;
  10.       let coefficient = 0;
  11.       let maxVotes;
  12.  
  13.       if (totalPost > 50) {
  14.         maxVotes = Math.max(post.upvotes, post.downvotes);
  15.         coefficient = Math.ceil(maxVotes * 0.25);
  16.       }
  17.  
  18.       output.push(post.upvotes + coefficient);
  19.       output.push(post.downvotes + coefficient);
  20.       output.push(post.upvotes - post.downvotes);
  21.       output.push(postRating(post));
  22.  
  23.       return output;
  24.     }
  25.   }
  26.  
  27.   function postRating(post) {
  28.     const upVotes = post.upvotes;
  29.     const downVotes = post.downvotes;
  30.     const totalVotes = upVotes + downVotes;
  31.     const balance = upVotes - downVotes;
  32.  
  33.     const ratings = {
  34.       new: totalVotes < 10,
  35.       hot: (upVotes / totalVotes) > 0.66,
  36.       controversial: balance >= 0 && (upVotes > 100 || downVotes > 100),
  37.       unpopular: balance < 0,
  38.     };
  39.  
  40.     for (const iter of Object.keys(ratings)) {
  41.       if (ratings[iter]) {
  42.         return iter;
  43.       }
  44.     }
  45.  
  46.     return 'new';
  47.   }
  48.  
  49.   return {
  50.     call,
  51.   };
  52. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement