Advertisement
Guest User

02. Song-Encryption-Without-Regex

a guest
Dec 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.   for (const line of input) {
  4.     if (line === 'end') {
  5.       break;
  6.     }
  7.  
  8.     let [ artist, song ] = line.split(':');
  9.  
  10.     if (!artistIsValid(artist) || !songIsValid(song)) {
  11.       console.log('Invalid input!');
  12.       continue;
  13.     }
  14.  
  15.     let key = artist.length;
  16.     let encryptedString = '';
  17.     for (const char of line) {
  18.       if (char === ':') {
  19.         encryptedString += '@';
  20.         continue;
  21.       }
  22.  
  23.       let asciiCode = char.charCodeAt(0);
  24.       if ((asciiCode >= 65 && asciiCode <= 90)) {
  25.         asciiCode += key;
  26.         if (asciiCode > 90) {
  27.           asciiCode -= 26;
  28.         }
  29.       }
  30.  
  31.       if ((asciiCode >= 97 && asciiCode <= 122)) {
  32.         asciiCode += key;
  33.         if (asciiCode > 122) {
  34.           asciiCode -= 26;
  35.         }
  36.       }
  37.  
  38.       encryptedString += String.fromCharCode(asciiCode);
  39.     }
  40.  
  41.     console.log(`Successful encryption: ${encryptedString}`);
  42.   }
  43.  
  44.   function artistIsValid(artist) {
  45.     let firstCharCode = artist.charCodeAt(0);
  46.     if (firstCharCode < 65 || firstCharCode > 90) {
  47.       return false;
  48.     }
  49.  
  50.     for (let index = 1; index < artist.length; index++) {
  51.       let asciiCode = artist.charCodeAt(index);
  52.       if ((asciiCode >= 97 && asciiCode <= 122)
  53.       || artist[index] === ' '
  54.       || artist[index] === '\'') {
  55.         continue;
  56.       } else {
  57.         return false;
  58.       }
  59.     }
  60.  
  61.     return true;
  62.   }
  63.  
  64.   function songIsValid(song) {
  65.     for (let index = 0; index < song.length; index++) {
  66.       let asciiCode = song.charCodeAt(index);
  67.       if ((asciiCode >= 65 && asciiCode <= 90)
  68.       || song[index] === ' ') {
  69.         continue;
  70.       } else {
  71.         return false;
  72.       }
  73.      
  74.     }
  75.  
  76.     return true;
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement