Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- let inputName = args.shift();
- let inputLine = args.shift();
- while (inputLine !== 'Sign up') {
- const [command, arg1, arg2] = inputLine.split(' ');
- switch (command) {
- case 'Case':
- if (arg1 === 'lower') {
- inputName = inputName.toLowerCase();
- console.log(inputName);
- } else if (arg1 === 'upper') {
- inputName = inputName.toUpperCase();
- console.log(inputName);
- }
- break;
- case 'Reverse':
- if (inputName.length > Number(arg2)) {
- const startIndex = Number(arg1);
- const endIndex = Number(arg2);
- const takeStr = inputName.slice(startIndex, endIndex + 1);
- const reverseStr = takeStr.split('').reverse().join('');
- console.log(reverseStr);
- }
- break;
- case 'Cut':
- if (inputName.includes(arg1)) {
- const startCh = arg1[0];
- const endCh = arg1[arg1.length - 1];
- const startIndex = inputName.indexOf(startCh);
- const endIndex = inputName.lastIndexOf(endCh);
- console.log(inputName.slice(0,startIndex) + inputName.slice(endIndex + 1));
- } else {
- console.log(`The word ${inputName} doesn't contain ${arg1}.`);
- }
- break;
- case 'Replace':
- inputName = inputName.replace(new RegExp(arg1, 'g'), '*');
- console.log(inputName);
- break;
- case 'Check':
- let charArr = inputName.split('');
- let isFound = charArr.some( ch => ch === arg1);
- if (isFound) {
- console.log('Valid.');
- } else {
- console.log(`Your username must contain ${arg1}.`);
- }
- break;
- default:
- break;
- }
- inputLine = args.shift();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement