Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. function solve(input) {
  2.  
  3. //escape spaces and put array into string with join
  4. input.pop();
  5. let mappedSong = [];
  6. input.forEach((line) => {
  7. let pattern = /^([A-Z][a-z'\s]+):([A-Z][A-Z\s]+[A-Z])$/gm;
  8. let result = pattern.exec(line);
  9. if (result) {
  10. let [artist, song] = line.split(':');
  11. let len = artist.length;
  12.  
  13. for (let i = 0; i < len; i++) {
  14. let currentChar = artist.charCodeAt(i);
  15. let newChar = currentChar + len;
  16.  
  17. if (currentChar === 32) {
  18. mappedSong.push(' ');
  19. continue;
  20. }
  21.  
  22. if (currentChar === 39) {
  23. mappedSong.push("'");
  24. continue;
  25. }
  26.  
  27. if(i!==0) {
  28. if (newChar > 122) {
  29. let calcChar = newChar - 122;
  30. let calcCharFinal = 96 + calcChar;
  31. newChar = calcCharFinal;
  32. }
  33. } else if (newChar > 90) {
  34. let calcChar = newChar - 90;
  35. let calcCharFinal = 65 + calcChar;
  36. newChar = calcChar;
  37. }
  38.  
  39. let mappedChar = String.fromCharCode(newChar);
  40. mappedSong.push(mappedChar);
  41.  
  42. }
  43.  
  44. mappedSong.push('@');
  45.  
  46. let lenSong = song.length;
  47.  
  48. for (let i = 0; i < song.length; i++) {
  49.  
  50.  
  51. let currentChar = song.charCodeAt(i);
  52. let newChar = currentChar + len;
  53.  
  54. if (currentChar === 32) {
  55. mappedSong.push(' ');
  56. continue;
  57. }
  58.  
  59. if (currentChar === 39) {
  60. mappedSong.push("'");
  61. continue;
  62. }
  63.  
  64. if (newChar > 90) {
  65. let calcChar = newChar - 90;
  66. let calcCharFinal = 64 + calcChar;
  67. newChar = calcCharFinal;
  68. }
  69.  
  70. let mappedChar = String.fromCharCode(newChar);
  71. mappedSong.push(mappedChar);
  72.  
  73. }
  74. }
  75.  
  76. if (mappedSong.join('') === '') {
  77. console.log(`Invalid input!`)
  78. } else {
  79. console.log(`Successful encryption: ${mappedSong.join('')}`);
  80. }
  81.  
  82. mappedSong = [];
  83. });
  84.  
  85.  
  86. }
  87.  
  88. solve([
  89. 'Linkin park:NUMB',
  90. 'Eminem:VENOM',
  91. 'Drake:NONSTOP',
  92. 'Adele:HELLO',
  93. 'end']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement