maymunskoa

01. Email Validator (Final Exam 071219)

Mar 14th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     const changeCase = (command, string)=>{
  4.         if(command === 'upper'){
  5.             return string.toUpperCase();
  6.         }else if(command === 'lower'){
  7.             return string.toLowerCase();
  8.         }
  9.     }
  10.  
  11.     const getDomain = (string, count)=>{
  12.         console.log(string.substring(string.length - count));
  13.          
  14.     }
  15.  
  16.     const getUsername = (string) =>{
  17.         let index = string.indexOf('@');
  18.         if(index === -1){
  19.         console.log("The email " + string + " doesn't contain the @ symbol.");
  20.         }else{
  21.             console.log(string.substring(0,index));
  22.         }
  23.     }
  24.  
  25.     const replaceWith = (string,char)=>{
  26.         let output = string;
  27.         while(output.indexOf(char) > -1){
  28.             output = output.replace(char,'-');
  29.         }
  30.         return output;
  31.     }
  32.  
  33.     const encrypt = (string) => {
  34.         let arr = string.split('');
  35.         let output = arr.map(letter=>{
  36.             return letter.charCodeAt()
  37.         }).join(' ');
  38.  
  39.         console.log(output);
  40.        
  41.     }
  42.  
  43.  
  44.     let email = input.shift();
  45.  
  46.     for (const line of input) {
  47.         if(line === 'Complete'){
  48.             break;
  49.         }
  50.         let [firstArg, secondArg] = line.split(' ');
  51.         switch(firstArg){
  52.             case 'Make':
  53.                 email = changeCase(secondArg.toLowerCase(), email);
  54.                 console.log(email);
  55.                 break;
  56.  
  57.             case 'GetDomain':
  58.                 getDomain(email,Number(secondArg));
  59.                 break;
  60.  
  61.             case 'GetUsername':
  62.                 getUsername(email);
  63.                 break;
  64.  
  65.             case 'Replace':
  66.               email = replaceWith(email, secondArg);
  67.               console.log(email);
  68.                 break;
  69.  
  70.             case 'Encrypt':
  71.                 encrypt(email);
  72.                 break;
  73.         }
  74.        
  75.  
  76.     }
  77.    
  78.    
  79. }
Add Comment
Please, Sign In to add comment