Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function votingSystem(action) {
  2.     let that = this;
  3.     let patcher = (function () {
  4.         function upvote() {
  5.             that.upvotes++;
  6.         }
  7.  
  8.         function downvote() {
  9.             that.downvotes++;
  10.         }
  11.  
  12.         function fakeRecords(num) {
  13.             return 0.25 * num;
  14.         }
  15.  
  16.         function calculateRating() {
  17.             let totalVotes = that.upvotes + that.downvotes;
  18.             if (totalVotes >= 10) {
  19.                 if (that.upvotes / totalVotes > 0.66) {
  20.                     return 'hot';
  21.                 } else if (that.upvotes - that.downvotes >= 0 && that.upvotes > 100 || that.downvotes > 100) {
  22.                     return 'controversial';
  23.                 } else if (that.upvotes - that.downvotes < 0) {
  24.                     return 'unpopular';
  25.                 }
  26.             }
  27.  
  28.             return 'new';
  29.         }
  30.  
  31.         function score() {
  32.             let up = that.upvotes;
  33.             let down = that.downvotes;
  34.  
  35.             let totalScore = that.upvotes - that.downvotes;
  36.             if (that.upvotes + that.downvotes > 50) {
  37.                 let additionalVotes = fakeRecords(Math.max(that.upvotes, that.downvotes));
  38.                 up = Math.ceil(that.upvotes + additionalVotes);
  39.                 down = Math.ceil(that.downvotes + additionalVotes);
  40.             }
  41.  
  42.             let rating = calculateRating();
  43.  
  44.             return [up, down, totalScore, rating];
  45.         }
  46.  
  47.         return {upvote, downvote, score};
  48.     })();
  49.  
  50.     return patcher[action]();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement