Advertisement
Lulunga

Final Exam 02. Song Encryption

Jul 27th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     for (let line of input) {
  3.         if (line === 'end') {
  4.             break;
  5.         }
  6.         let pattern = /(?<artist>^[A-Z][a-z'|\s]+[a-z]*):(?<song>[A-Z\s]+)$/gm;
  7.         let result = pattern.exec(line);
  8.         if (result) {
  9.             let artist = result.groups.artist;
  10.             let song = result.groups.song;
  11.             let key = artist.length;
  12.             let songAndArtist = artist + song;
  13.             songAndArtist = songAndArtist
  14.                 .split('')
  15.                 .map(function (e) {
  16.  
  17.                     if (e === " ") {
  18.                         return e = ' ';
  19.                     } else if (e === "'") {
  20.                         return e = "'";
  21.                     } else {
  22.                         let updatedKey = e.charCodeAt(0) + key;
  23.                         if (updatedKey > 'Z'.charCodeAt(0) && e.charCodeAt(0) <= 'Z'.charCodeAt(0)) {
  24.  
  25.                             let difference = updatedKey - 'Z'.charCodeAt(0);
  26.                             updatedKey = 64 + difference;
  27.                             return e = String.fromCharCode(updatedKey);
  28.  
  29.                         } else if (updatedKey > 'z'.charCodeAt(0)) {
  30.                             let difference = updatedKey - 'z'.charCodeAt(0);
  31.                             updatedKey = 96 + difference;
  32.                             return e = String.fromCharCode(updatedKey);
  33.  
  34.                         } else {
  35.                             return e = String.fromCharCode(updatedKey);
  36.                         }
  37.                     }
  38.  
  39.                 });
  40.  
  41.  
  42.             songAndArtist.splice(key, 0, '@');
  43.             console.log(`Successful encryption: ${songAndArtist.join('')}`);
  44.  
  45.         } else {
  46.             console.log("Invalid input!");
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement