marian74

02. Song Encryption

Jul 25th, 2019
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function songEncryption(data) {
  2.     let info = data.shift();
  3.     while (info != 'end') {
  4.         let pattern = /^(?<name>[A-Z][a-z ']+):[A-Z ]+$/g;
  5.         let result = pattern.exec(info);
  6.         // console.log(result);
  7.         if (result !== null) {
  8.             let name = result.groups.name;
  9.             let key = name.length;
  10.             let encryptedMessage = '';
  11.             for (let i = 0; i < result[0].length; i++) {
  12.                 let asciiChar = result[0][i].charCodeAt();
  13.                 let newAsciiChar = 0;
  14.                 if (65<=asciiChar && asciiChar<=90) {
  15.                     newAsciiChar = asciiChar + key;
  16.                     if (newAsciiChar > 90) {
  17.                         newAsciiChar -= 26;
  18.                     }
  19.                    
  20.                 } else if (97<=asciiChar && asciiChar<=122) {
  21.                     newAsciiChar = asciiChar + key;
  22.                     if (newAsciiChar > 122) {
  23.                         newAsciiChar -= 26;
  24.                     }
  25.                 } else if (asciiChar === 58) {
  26.                     newAsciiChar = 64;
  27.                 } else if (asciiChar === 39 || asciiChar === 32) {
  28.                     newAsciiChar = asciiChar;
  29.                 }
  30.                 encryptedMessage += String.fromCharCode(newAsciiChar);
  31.             }
  32.             console.log(`Successful encryption: ${encryptedMessage}`);
  33.            
  34.         } else {
  35.             console.log('Invalid input!');
  36.         }
  37.  
  38.         info = data.shift();
  39.     }
  40. }
Add Comment
Please, Sign In to add comment