Advertisement
kstoyanov

Username js exam

Aug 14th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   let inputName = args.shift();
  3.  
  4.   let inputLine = args.shift();
  5.  
  6.   while (inputLine !== 'Sign up') {
  7.     const [command, arg1, arg2] = inputLine.split(' ');
  8.  
  9.  
  10.     switch (command) {
  11.       case 'Case':
  12.  
  13.         if (arg1 === 'lower') {
  14.           inputName = inputName.toLowerCase();
  15.           console.log(inputName);
  16.         } else if (arg1 === 'upper') {
  17.           inputName = inputName.toUpperCase();
  18.           console.log(inputName);
  19.         }
  20.  
  21.         break;
  22.       case 'Reverse':
  23.  
  24.  
  25.         if (inputName.length > Number(arg2)) {
  26.           const startIndex = Number(arg1);
  27.           const endIndex = Number(arg2);
  28.  
  29.           const takeStr = inputName.slice(startIndex, endIndex + 1);
  30.           const reverseStr = takeStr.split('').reverse().join('');
  31.           console.log(reverseStr);
  32.         }
  33.  
  34.         break;
  35.       case 'Cut':
  36.         if (inputName.includes(arg1)) {
  37.          
  38.           const startCh = arg1[0];
  39.           const endCh = arg1[arg1.length - 1];
  40.           const startIndex = inputName.indexOf(startCh);
  41.           const endIndex = inputName.lastIndexOf(endCh);
  42.           console.log(inputName.slice(0,startIndex) + inputName.slice(endIndex + 1));
  43.         } else {
  44.           console.log(`The word ${inputName} doesn't contain ${arg1}.`);
  45.        }
  46.        break;
  47.      case 'Replace':
  48.        inputName = inputName.replace(new RegExp(arg1, 'g'), '*');
  49.        console.log(inputName);
  50.        break;
  51.      case 'Check':
  52.  
  53.        let charArr = inputName.split('');
  54.        let isFound = charArr.some( ch => ch === arg1);        
  55.  
  56.        if (isFound) {
  57.          console.log('Valid.');
  58.        } else {
  59.          console.log(`Your username must contain ${arg1}.`);
  60.        }
  61.        break;
  62.      default:
  63.        break;
  64.    }
  65.  
  66.    inputLine = args.shift();
  67.  }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement