Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- const namePattern = /^[A-Z][a-z ']+$/m;
- const songPattern = /^[A-Z\s]+$/gm;
- const encrypt = (name, key) => {
- let str = "";
- for (const letter of name) {
- let newcode = letter.charCodeAt() + key;
- if (letter.charCodeAt() > 64 && letter.charCodeAt() < 91) {
- //A-Z
- if (newcode > 90) {
- let diff = newcode - 90;
- newcode = 64 + diff;
- }
- str += String.fromCharCode(newcode);
- } else if (letter.charCodeAt() > 96 && letter.charCodeAt() < 123) {
- //a-z
- if (newcode > 122) {
- let diff = newcode - 122;
- newcode = 96 + diff;
- }
- str += String.fromCharCode(newcode);
- } else {
- str += letter;
- }
- }
- return str;
- };
- for (const line of input) {
- if (line === "end") {
- break;
- }
- let [artist, song] = line.split(":");
- artist = artist.trim();
- song = song.trim();
- if (
- artist.match(namePattern) !== null &&
- song.match(songPattern) !== null
- ) {
- let key = artist.length;
- let encName = encrypt(artist, key);
- let encSong = encrypt(song, key);
- console.log(`Successful encryption: ${encName}@${encSong}`);
- } else {
- console.log(`Invalid input!`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment