Advertisement
nikolayneykov

Untitled

Mar 7th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let chat = [];
  3.  
  4.     for (let line of arr) {
  5.         let tokens = line.split(' ');
  6.         let command = tokens[0];
  7.         let message = tokens[1];
  8.  
  9.         if (command === 'Chat') {
  10.             chat.push(message);
  11.         } else if (command === 'Delete') {
  12.             chat = chat.filter(x => x !== message);
  13.         } else if (command === 'Edit') {
  14.             let newMessage = tokens[2];
  15.             chat.map((x, i, chat) => x === message ? chat[i] = newMessage : chat[i] = x);
  16.         } else if (command === 'Pin') {
  17.             let messagesToPin = chat.filter(x => x === message);
  18.             chat = chat.filter(x => x !== message);
  19.             chat.push(...messagesToPin);
  20.         } else if (command === 'Spam') {
  21.             chat.push(...tokens.slice(1));
  22.         }
  23.     }
  24.  
  25.     console.log(chat.join('\n'));
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement