Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution(input) {
- let username = input.shift();
- let token = input.shift();
- while (token !== `Sign up`) {
- let [comand, checkOne, checkTwo] = token.split(` `);
- if (comand === `Case` && checkOne === `lower`) {
- username = username.toLowerCase();
- console.log(username);
- } else if (comand === `Case` && checkOne === `upper`) {
- username = username.toUpperCase();
- console.log(username);
- } else if (comand === `Reverse`) {
- reverse(username, checkOne, checkTwo);
- } else if (comand === `Cut`) {
- username = cut(username, checkOne);
- } else if (comand === `Replace`) {
- username = replace(username, checkOne);
- } else if (comand === `Check`) {
- check(username, checkOne);
- }
- token = input.shift();
- }
- function reverse(string, startIndex, endIndex) {
- startIndex = Number(startIndex);
- endIndex = Number(endIndex);
- if (
- startIndex >= 0 &&
- startIndex < string.length &&
- endIndex >= 0 &&
- endIndex < string.length
- ) {
- let subst = string
- .substring(startIndex, endIndex + 1)
- .split(``)
- .reverse()
- .join(``);
- console.log(subst);
- }
- return string;
- }
- function cut(string, substring) {
- if (string.includes(substring)) {
- string = string.replace(substring, ``);
- console.log(string);
- } else {
- console.log(`The word ${string} doesn't contain ${substring}.`);
- }
- return string;
- }
- function replace(string, char) {
- while (string.includes(char)) {
- string = string.replace(char, `*`);
- }
- console.log(string);
- return string;
- }
- function check(string, char) {
- if (string.includes(char)) {
- console.log(`Valid`);
- } else {
- console.log(`Your username must contain ${char}.`);
- }
- return string;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment