Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let post = {
- id: '3',
- author: 'emil',
- content: 'wazaaaaa',
- upvotes: 100,
- downvotes: 100
- }
- function solution(post, command) {
- const thereIsNoMajority = () => (post.upvotes / post.upvotes + post.downvotes) * 100 <= 66 &&
- (post.downvotes / post.upvotes + post.downvotes) * 100 <= 66
- const eitherVotesAreMoreThan100 = () => post.upvotes > 100 || post.downvotes > 100
- let commands = {
- 'upvote': () => post.upvote++,
- 'downvote': () => post.downvotes++,
- 'score': () => {
- let score = [post.upvotes, post.downvotes]
- if (post.upvotes + post.downvotes > 50) {
- let number = Math.ceil(0.25 * Math.max(post.upvotes, post.downvotes))
- score = score.map(x => x + number)
- }
- let balance = post.upvotes - post.downvotes;
- score.push(balance)
- let rating;
- let upvotesPercents = (post.upvotes / post.upvotes + post.downvotes) * 100
- if (post.upvotes + post.downvotes < 10) {
- rating = 'new'
- } else if (balance > 0 && upvotesPercents > 66) {
- rating = 'hot';
- } else if (thereIsNoMajority() && balance >= 0 && eitherVotesAreMoreThan100()) {
- rating = 'controversal';
- } else if (balance < 0) {
- rating = 'unpopular';
- } else {
- rating = 'new'
- }
- score.push(rating)
- return score;
- }
- }
- return commands[command]();
- }
- solution.call(post, 'upvote');
- solution.call(post, 'downvote');
- let score = solution.call(post, 'score');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement