krasizorbov

Username

Jul 20th, 2020
2,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(input) {
  2.   let username = input.shift();
  3.   let token = input.shift();
  4.  
  5.   while (token !== `Sign up`) {
  6.     let [comand, checkOne, checkTwo] = token.split(` `);
  7.  
  8.     if (comand === `Case` && checkOne === `lower`) {
  9.       username = username.toLowerCase();
  10.       console.log(username);
  11.     } else if (comand === `Case` && checkOne === `upper`) {
  12.       username = username.toUpperCase();
  13.       console.log(username);
  14.     } else if (comand === `Reverse`) {
  15.       reverse(username, checkOne, checkTwo);
  16.     } else if (comand === `Cut`) {
  17.       username = cut(username, checkOne);
  18.     } else if (comand === `Replace`) {
  19.       username = replace(username, checkOne);
  20.     } else if (comand === `Check`) {
  21.       check(username, checkOne);
  22.     }
  23.  
  24.     token = input.shift();
  25.   }
  26.  
  27.   function reverse(string, startIndex, endIndex) {
  28.     startIndex = Number(startIndex);
  29.     endIndex = Number(endIndex);
  30.  
  31.     if (
  32.       startIndex >= 0 &&
  33.       startIndex < string.length &&
  34.       endIndex >= 0 &&
  35.       endIndex < string.length
  36.     ) {
  37.       let subst = string
  38.         .substring(startIndex, endIndex + 1)
  39.         .split(``)
  40.         .reverse()
  41.         .join(``);
  42.       console.log(subst);
  43.     }
  44.  
  45.     return string;
  46.   }
  47.  
  48.   function cut(string, substring) {
  49.     if (string.includes(substring)) {
  50.       string = string.replace(substring, ``);
  51.  
  52.       console.log(string);
  53.     } else {
  54.       console.log(`The word ${string} doesn't contain ${substring}.`);
  55.    }
  56.  
  57.    return string;
  58.  }
  59.  
  60.  function replace(string, char) {
  61.    while (string.includes(char)) {
  62.      string = string.replace(char, `*`);
  63.    }
  64.    console.log(string);
  65.    return string;
  66.  }
  67.  
  68.  function check(string, char) {
  69.    if (string.includes(char)) {
  70.      console.log(`Valid`);
  71.    } else {
  72.      console.log(`Your username must contain ${char}.`);
  73.    }
  74.    return string;
  75.  }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment