Advertisement
Todorov_Stanimir

02. Song Encryption Final Exam Preparation - 24 July 2019

Jul 24th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function songEncryption(input) {
  2.     let pattern = /^([A-Z][a-z' ]+):([A-Z ]+)$/g;
  3.  
  4.     while ((line = input.shift()) !== 'end') {
  5.         if (line.match(pattern)) {
  6.             let fordecription = line.match(pattern)[0];
  7.             let key = fordecription.split(':')[0].length;
  8.             let decrepted = fordecription.split('').map(el => {
  9.                 if (el.charCodeAt() >= 65 && el.charCodeAt() <= 90) {
  10.                     return String.fromCharCode(65 + ((el.charCodeAt() + key) % 65) % 26);
  11.                 } else if (el.charCodeAt() >= 97 && el.charCodeAt() <= 122) {
  12.                     return String.fromCharCode(97 + ((el.charCodeAt() + key) % 97) % 26);
  13.                 } else if (el.charCodeAt() === 58) {
  14.                     return String.fromCharCode(64);
  15.                 } else {
  16.                     return el
  17.                 }
  18.             }).join('');
  19.             console.log(`Successful encryption: ${decrepted}`);
  20.         } else {
  21.             console.log('Invalid input!')
  22.         }
  23.     }
  24. }
  25. songEncryption(['Eminem:VENOM',
  26.     'Linkin park:NUMB',
  27.     'Drake:NONSTOP',
  28.     'Adele:HELLO',
  29.     'end']);
  30. songEncryption(['Michael Jackson:ANOTHER PART OF ME',
  31.     'Adele:ONE AND ONLY',
  32.     'Guns n\'roses:NOVEMBER RAIN',
  33.     'Christina Aguilera: HuRt',
  34.     'end']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement