Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let post = {
  2.     id: '3',
  3.     author: 'emil',
  4.     content: 'wazaaaaa',
  5.     upvotes: 100,
  6.     downvotes: 100
  7. }
  8.  
  9. function solution(post, command) {
  10.  
  11.     const thereIsNoMajority = () => (post.upvotes / post.upvotes + post.downvotes) * 100 <= 66 &&
  12.         (post.downvotes / post.upvotes + post.downvotes) * 100 <= 66
  13.  
  14.     const eitherVotesAreMoreThan100 = () => post.upvotes > 100 || post.downvotes > 100
  15.  
  16.     let commands = {
  17.         'upvote': () => post.upvote++,
  18.         'downvote': () => post.downvotes++,
  19.         'score': () => {
  20.  
  21.             let score = [post.upvotes, post.downvotes]
  22.  
  23.             if (post.upvotes + post.downvotes > 50) {
  24.                 let number = Math.ceil(0.25 * Math.max(post.upvotes, post.downvotes))
  25.                 score = score.map(x => x + number)
  26.             }
  27.  
  28.             let balance = post.upvotes - post.downvotes;
  29.  
  30.             score.push(balance)
  31.  
  32.             let rating;
  33.  
  34.             let upvotesPercents = (post.upvotes / post.upvotes + post.downvotes) * 100
  35.  
  36.             if (post.upvotes + post.downvotes < 10) {
  37.                 rating = 'new'
  38.             } else if (balance > 0 && upvotesPercents > 66) {
  39.                 rating = 'hot';
  40.             } else if (thereIsNoMajority() && balance >= 0 && eitherVotesAreMoreThan100()) {
  41.                 rating = 'controversal';
  42.             } else if (balance < 0) {
  43.                 rating = 'unpopular';
  44.             } else {
  45.                 rating = 'new'
  46.             }
  47.  
  48.             score.push(rating)
  49.  
  50.             return score;
  51.         }
  52.     }
  53.  
  54.     return commands[command]();
  55. }
  56.  
  57.  
  58. solution.call(post, 'upvote');
  59. solution.call(post, 'downvote');
  60. let score = solution.call(post, 'score');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement