SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function demo(input) {
  2.     let users = {};
  3.  
  4.     while (input.length > 0) {
  5.         let [command, value, count] = input.shift().split(": ");
  6.  
  7.         if (command === "New follower" && !users.hasOwnProperty(value)) {
  8.             users[value] = { "likes": 0, "comments": 0 };
  9.         } else if (command === "Like") {
  10.             if (users.hasOwnProperty(value)) {
  11.                 users[value].likes += +count;
  12.             } else {
  13.                 users[value] = { "likes": count, "comments": 0 };
  14.             }
  15.         } else if (command === "Comment") {
  16.             if (users.hasOwnProperty(value)) {
  17.                 users[value].comments += 1;
  18.             } else {
  19.                 users[value] = { "likes": 0, "comments": 1 };
  20.             }
  21.         } else if (command === "Blocked") {
  22.             if (users.hasOwnProperty(value)) {
  23.                 delete users[value];
  24.             } else {
  25.                 console.log(`${value} doesn't exist.`);
  26.            }
  27.        }
  28.    }
  29.    console.log(`${Object.keys(users).length} followers`);
  30.    Object.entries(users).sort((a, b) => b[1].likes - a[1].likes || a[0].localeCompare(b[0]))
  31.        .forEach(element => console.log(`${element[0]}: ${+element[1].likes + element[1].comments}`));
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top