Liliana797979

problem 2_4 - mid exam

Jul 22nd, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function solve(arr) {
  3.     let oldSchool = arr.shift().split(' | ')
  4.  
  5.     for (let line of arr) {
  6.         let [command, ...args] = line.split(' ')
  7.         if (command == 'Join') {
  8.             if (!oldSchool.includes(args[0])) {
  9.                 oldSchool.push(args[0])
  10.             }
  11.         } else if (command == 'Drop') {
  12.             if (oldSchool.includes(args[0])) {
  13.                 let index = oldSchool.indexOf(args[0])
  14.                 oldSchool.splice(index, 1)
  15.             }
  16.         } else if (command == 'Replace') {
  17.             if (oldSchool.includes(args[0]) && !oldSchool.includes(args[1])) {
  18.                 let index = oldSchool.indexOf(args[0])
  19.                 oldSchool.splice(index, 1, args[1])
  20.             }
  21.         } else {
  22.             console.log(oldSchool.join(' '));
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment