SHARE
TWEET

asassds

a guest Mar 26th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let text = input.shift();
  3.  
  4.     const isValidIndex = (index) => {
  5.         return index >= 0 && index < text.length;
  6.     };
  7.  
  8.     const replace = (currentChar, newChar) => {
  9.         while (text.search(currentChar) !== -1) {
  10.             text = text.replace(currentChar, newChar)
  11.         }
  12.  
  13.         return text;
  14.     }
  15.  
  16.     const cut = (startIndex, endIndex) => {
  17.         startIndex = Number(startIndex);
  18.         endIndex = Number(endIndex);
  19.  
  20.         if (isValidIndex(startIndex) && isValidIndex(endIndex)) {
  21.             text = text.substring(0, startIndex) + text.substring(endIndex + 1);
  22.  
  23.             return text;
  24.         } else {
  25.             return 'Invalid indexes!';
  26.         }
  27.     }
  28.  
  29.     const make = (type) => {
  30.         type === 'Upper'
  31.             ? text = text.toUpperCase()
  32.             : text = text.toLowerCase();
  33.  
  34.         return text;
  35.     }
  36.  
  37.     const check = (string) => {
  38.         return text.search(string) !== -1
  39.                 ? `Message contains ${string}`
  40.                 : `Message doesn't contain ${string}`;
  41.    }
  42.  
  43.    const sum = (startIndex, endIndex) => {
  44.        startIndex = Number(startIndex);
  45.        endIndex = Number(endIndex);
  46.  
  47.        if (isValidIndex(startIndex) && isValidIndex(endIndex)) {
  48.            return [...text.substring(startIndex, endIndex + 1)]
  49.                    .reduce((acc, current) => {
  50.                        return acc + current.charCodeAt(0);
  51.                    }, 0)
  52.        } else {
  53.            return 'Invalid indexes!';
  54.        }
  55.    }
  56.  
  57.    const cmdParser = {
  58.        replace,
  59.        make,
  60.        cut,
  61.        check,
  62.        sum
  63.    };
  64.  
  65.    input
  66.        .forEach(line => {
  67.            if (line !== 'Finish') {
  68.                const tokens = line.split(' ');
  69.                const cmd = tokens.shift().toLowerCase();
  70.  
  71.                console.log(
  72.                    cmdParser[cmd](...tokens)
  73.                );
  74.            }
  75.        });
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top