Advertisement
Guest User

02. Song-Encryption-With-Regex

a guest
Dec 18th, 2018
104
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 (!/^[A-Z][a-z\s\']+$/g.test(artist) || !/^[A-Z\s]+$/g.test(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.  
  45. // solve(
  46. // [
  47. //   'Eminem:VENOM',
  48. //   'Linkin park:NUMB',
  49. //   'Drake:NONSTOP',
  50. //   'Adele:ROLLING IN THE DEEP',
  51. //   'end'
  52. // ]
  53. // );
  54.  
  55. // solve([
  56. //   'Michael Jackson:ANOTHER PART OF ME',
  57. //   'Adele:ONE AND ONLY',
  58. //   'Guns n\'roses:NOVEMBER RAIN',
  59. //   'Christina Aguilera: HuRt',
  60. //   'end'
  61. // ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement