SHARE
TWEET

03. Followers

a guest Dec 3rd, 2019 79 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] = {};
  14.                 users[value].likes = count;
  15.                 users[value].comments = 0;
  16.             }
  17.         } else if (command === "Comment") {
  18.             if (users.hasOwnProperty(value)) {
  19.                 users[value].comments += 1;
  20.             } else {
  21.                 users[value] = {};
  22.                 users[value].likes = 0;
  23.                 users[value].comments = 1;
  24.             }
  25.         } else if (command === "Blocked") {
  26.             if (users.hasOwnProperty(value)) {
  27.                 delete users[value];
  28.             } else {
  29.                 console.log(`${value} doesn't exist.`);
  30.            }
  31.        }
  32.    }
  33.    console.log(`${Object.keys(users).length} followers`);
  34.    for (const key in users) {
  35.        users[key] = +users[key].likes + users[key].comments;
  36.    }
  37.    Object.entries(users).sort().sort((a, b) => b[1] - a[1])
  38.        .forEach(element => console.log(`${element[0]}: ${element[1]}`));
  39. }
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