Advertisement
Liliana797979

problem 2 - mid exam

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