Advertisement
Spocoman

03. Phone Shop

Feb 18th, 2024 (edited)
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function phoneShop(input) {
  2.     let phones = input.shift().split(', ');
  3.  
  4.     while (input[0] !== 'End') {
  5.         let [command, phone] = input.shift().split(' - ');
  6.  
  7.         if (command === 'Add' && !phones.includes(phone)) {
  8.             phones.push(phone);
  9.         } else if (command === 'Remove' && phones.includes(phone)) {
  10.             phones.splice(phones.indexOf(phone, 1));
  11.         } else if (command === 'Bonus phone') {
  12.             let [oldPhone, newPhone] = phone.split(':');
  13.             if (phones.includes(oldPhone)) {
  14.                 phones.splice(phones.indexOf(oldPhone) + 1, 0, newPhone);
  15.             }
  16.         } else if (command === 'Last' && phones.includes(phone)) {
  17.             phones.push(phone);
  18.             phones.splice(phones.indexOf(phone), 1);
  19.         }
  20.     }
  21.  
  22.     console.log(phones.join(', '));
  23.     return;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement