Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- for (let line of input) {
- if (line === 'end') {
- break;
- }
- let pattern = /(?<artist>^[A-Z][a-z'|\s]+[a-z]*):(?<song>[A-Z\s]+)$/gm;
- let result = pattern.exec(line);
- if (result) {
- let artist = result.groups.artist;
- let song = result.groups.song;
- let key = artist.length;
- let songAndArtist = artist + song;
- songAndArtist = songAndArtist
- .split('')
- .map(function (e) {
- if (e === " ") {
- return e = ' ';
- } else if (e === "'") {
- return e = "'";
- } else {
- let updatedKey = e.charCodeAt(0) + key;
- if (updatedKey > 'Z'.charCodeAt(0) && e.charCodeAt(0) <= 'Z'.charCodeAt(0)) {
- let difference = updatedKey - 'Z'.charCodeAt(0);
- updatedKey = 64 + difference;
- return e = String.fromCharCode(updatedKey);
- } else if (updatedKey > 'z'.charCodeAt(0)) {
- let difference = updatedKey - 'z'.charCodeAt(0);
- updatedKey = 96 + difference;
- return e = String.fromCharCode(updatedKey);
- } else {
- return e = String.fromCharCode(updatedKey);
- }
- }
- });
- songAndArtist.splice(key, 0, '@');
- console.log(`Successful encryption: ${songAndArtist.join('')}`);
- } else {
- console.log("Invalid input!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement