Advertisement
kstoyanov

01. World Tour js exam

Aug 9th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arg) {
  2.   let dest = arg.shift();
  3.   let commandLine = arg.shift();
  4.  
  5.   function insert(str, index, value) {
  6.     return str.substr(0, index) + value + str.substr(index);
  7.   }
  8.  
  9.  
  10.   while (commandLine !== 'Travel') {
  11.     const [command, firstArument, secondArgument] = commandLine.split(':');
  12.  
  13.     switch (command) {
  14.       case 'Add Stop': {
  15.         const indexStr = Number(firstArument);
  16.         const takeChar = dest[indexStr];
  17.         if (dest.indexOf(takeChar) !== -1) {
  18.           dest = insert(dest, indexStr, secondArgument);
  19.          
  20.         }else{
  21.  
  22.           console.log(`${dest}`);
  23.         }
  24.        
  25.       }
  26.       case 'Remove Stop': {
  27.         const startIndex = Number(firstArument);
  28.         const endIndex = Number(secondArgument);
  29.  
  30.         if (dest.indexOf(dest[startIndex]) !== -1 && dest.indexOf(dest[endIndex]) !== -1) {
  31.           dest = dest.slice(0, startIndex) + dest.slice(endIndex + 1);          
  32.          
  33.         }
  34.         console.log(`${dest}`);
  35.        
  36.         break;
  37.       }
  38.       case 'Switch': {
  39.         if (dest.includes(firstArument)) {
  40.           dest = dest.replace(firstArument, secondArgument);
  41.          
  42.         }
  43.         console.log(`${dest}`);
  44.         break;
  45.       }
  46.      
  47.     }
  48.  
  49.     commandLine = arg.shift();
  50.   }
  51.  
  52.   console.log(`Ready for world tour! Planned stops: ${dest}`);
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement