AlexanderHristov

02. Song Encryption

Dec 17th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function songEncryption(inputArr) {
  2.  
  3.     let artistValidator = /^[A-Z][a-z\s']+$/g;
  4.     let songValidator = /^[A-Z\s]+[^!@#$%^&*()_\-=a-z0-9]$/g;
  5.  
  6.     for (let string of inputArr) {
  7.  
  8.         let encryptedArr = [];
  9.         let result = '';
  10.  
  11.         if (string === 'end') {
  12.             break;
  13.         }
  14.  
  15.         let [artist, song] = string.split(':');
  16.  
  17.         if (artist.match(artistValidator) !== null && song.match(songValidator) !== null) {
  18.  
  19.             let encryptionKey = artist.length;
  20.  
  21.             for (let char of string) {
  22.  
  23.                 let ASCII = char.charCodeAt(0);
  24.                 let newCharCode = '';
  25.  
  26.                 if (char === ':') {
  27.                     newCharCode = 64;
  28.  
  29.                 } else if (ASCII >= 97 && ASCII <= 122) {
  30.  
  31.                     newCharCode = ASCII + encryptionKey;
  32.  
  33.                     if (newCharCode > 122) {
  34.  
  35.                         let x = newCharCode - 122;
  36.                         newCharCode = 96 + x;
  37.                     } // lowerCase
  38.  
  39.                 } else if (ASCII >= 65 && ASCII <= 90) {
  40.  
  41.                     newCharCode = ASCII + encryptionKey;
  42.  
  43.                     if (newCharCode > 90) {
  44.  
  45.                         let x = newCharCode - 90;
  46.                         newCharCode = 64 + x;
  47.                     } // upperCase
  48.  
  49.                 } else if (char === ' ' || char === '\'') {
  50.  
  51.                     newCharCode = ASCII;
  52.                 } // space or `
  53.  
  54.                 let encryptedChar = String.fromCharCode(newCharCode);
  55.                 encryptedArr.push(encryptedChar);
  56.             }
  57.  
  58.             result = encryptedArr.join('');
  59.             console.log(`Successful encryption: ${result}`);
  60.  
  61.         } else {
  62.  
  63.             console.log('Invalid input!');
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment