Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function solution() {
- function call(post, command) {
- if (command === 'upvote') {
- post.upvotes++;
- } else if (command === 'downvote') {
- post.downvotes++;
- } else if (command === 'score') {
- const output = [];
- const totalPost = post.upvotes + post.downvotes;
- let coefficient = 0;
- let maxVotes;
- if (totalPost > 50) {
- maxVotes = Math.max(post.upvotes, post.downvotes);
- coefficient = Math.ceil(maxVotes * 0.25);
- }
- output.push(post.upvotes + coefficient);
- output.push(post.downvotes + coefficient);
- output.push(post.upvotes - post.downvotes);
- output.push(postRating(post));
- return output;
- }
- }
- function postRating(post) {
- const upVotes = post.upvotes;
- const downVotes = post.downvotes;
- const totalVotes = upVotes + downVotes;
- const balance = upVotes - downVotes;
- const ratings = {
- new: totalVotes < 10,
- hot: (upVotes / totalVotes) > 0.66,
- controversial: balance >= 0 && (upVotes > 100 || downVotes > 100),
- unpopular: balance < 0,
- };
- for (const iter of Object.keys(ratings)) {
- if (ratings[iter]) {
- return iter;
- }
- }
- return 'new';
- }
- return {
- call,
- };
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement