maymunskoa

Message Translator (FinalExam 071219)

Mar 13th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let count = Number(input.shift());
  4. let isValid = false;
  5. let command = '';
  6. let message = '';
  7.  
  8. const decypher = (msg)=>{
  9.     let arr = msg.split('');
  10.     return arr.map(item=>{
  11.         return item.charCodeAt();
  12.     }).join(' ');
  13. };
  14.  
  15.  
  16. const stripEnds = (string)=>{
  17.     return string.substring(1,string.length-1);
  18. }
  19.  
  20.  
  21. const lineValid = (line)=>{
  22.     return line.includes('!:[');
  23. }
  24.  
  25. const commandLineValid = (commandLine)=>{
  26.     return (commandLine.startsWith('!') && commandLine.endsWith('!'));
  27. }
  28.  
  29. const onlyLowerCase = (word)=>{
  30.     for (const letter of word) {
  31.         if(letter.charCodeAt()<97 || letter.charCodeAt()>122){
  32.             return false;
  33.         }
  34.     }
  35.     return true;
  36. }
  37.  
  38. const commandValid = (command)=>{
  39.     if((command[0].charCodeAt()>=65 && command[0].charCodeAt()<=90) && onlyLowerCase(command.substring(1)) && command.length>=3 ){
  40.         return true;
  41.     }else{
  42.         return false;
  43.     }
  44. }
  45.  
  46. const isOnlyAlphabetical = (string)=>{
  47.     for (const letter of string) {
  48.         if((letter.charCodeAt() < 65 || letter.charCodeAt() > 122) || (letter.charCodeAt() > 90 && letter.charCodeAt() < 97)){
  49.             return false;
  50.         }
  51.     }
  52.     return true;
  53. }
  54.  
  55.  
  56. const isValidMessageLine = (line)=>{
  57.     return (line.startsWith('[') && line.endsWith(']'))
  58. }
  59.  
  60. const messageIsValid = (message) =>{
  61.     return (isOnlyAlphabetical(message) && message.length>=8);
  62. }
  63.  
  64.  
  65.  
  66. for (const line of input) {
  67.     if(lineValid(line)){
  68.        let [commandLine, messageLine] = line.split(':');
  69.        if(commandLineValid(commandLine) && isValidMessageLine(messageLine)){
  70.              command = stripEnds(commandLine);
  71.              message = stripEnds(messageLine);  
  72.         if(commandValid(command) && messageIsValid(message)){
  73.             isValid = true;
  74.            
  75.         }
  76.        }
  77.     }
  78.  
  79.  
  80.     console.log( isValid === false ? `The message is invalid` : `${command}: ${decypher(message)}`);
  81.     isValid = false;
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment