Advertisement
fbinnzhivko

04.01 Radical_Marketing

Oct 16th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(input) {
  2.     let people = [];
  3.     for (let i = 0; i < input.length; i++) {
  4.         let parts = input[i].split('-');
  5.         if (parts.length == 1) {
  6.             let obj = {Name: parts[0], Subscribers: [], SubsribedTo: 0};
  7.             people.push(obj);
  8.         }
  9.         else {
  10.             let firstPerson = people.filter(x=> x.Name == parts[0])[0];
  11.             let secondPerson = people.filter(x=> x.Name == parts[1])[0];
  12.  
  13.             if (firstPerson === undefined || secondPerson === undefined) {
  14.                 continue;
  15.             }
  16.             secondPerson.Subscribers.push(firstPerson);
  17.             firstPerson.SubsribedTo++;
  18.         }
  19.     }
  20.     let personWithMostSubscribers = people[0];
  21.     for (let i = 1; i < people.length; i++) {
  22.         if (people[i].Subscribers.length > personWithMostSubscribers.Subscribers.length) {
  23.             personWithMostSubscribers = people[i];
  24.         }
  25.         else if (people[i].Subscribers.length === personWithMostSubscribers.Subscribers.length) {
  26.             if (people[i].SubsribedTo > personWithMostSubscribers.SubsribedTo) {
  27.                 personWithMostSubscribers = people[i];
  28.             }
  29.         }
  30.     }
  31.     console.log(personWithMostSubscribers.Name);
  32.     for (let i = 0; i < personWithMostSubscribers.Subscribers.length; i++) {
  33.         console.log(i + 1 + '. ' + personWithMostSubscribers.Subscribers[i].Name);
  34.     }
  35. }
  36. solution(['A', 'B', 'C', 'D', 'A-B', 'B-A', 'C-A', 'D-A']);
  37. solution(['J', 'G', 'P', 'R', 'C', 'J-G', 'G-J', 'P-R', 'R-P', 'C-J']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement