CoDEVIANT #10 (4/2/19)
I was going to make an entry yesterday, but somehow I felt that given the holiday I needed to save my foolishness for today…I’m actually in rehearsal and need to run on stage in a second *quite literally*. Come see Don Giovanni presented by Opera Bend if you happen to be in Bend, Oregon. April 5, 6, and 7th!
Problem 1: Encrypt this!
Instructions:
Description:
Encrypt this!
You want to create secret messages which can be deciphered by the Decipher this! kata. Here are the conditions:
- Your message is a string containing space separated words.
- You need to encrypt each word in the message using the following rules:
- The first letter needs to be converted to its ASCII code.
- The second letter needs to be switched with the last letter
- Keepin’ it simple: There are no special characters in input.
Examples:
encryptThis("Hello") === "72olle"
encryptThis("good") === "103doo"
encryptThis("hello world") === "104olle 119drlo"
(https://www.codewars.com/kata/5848565e273af816fb000449/train/javascript)
How Adrian Solved It:
var encryptThis = function(text) {let answer = []; function go(array){ let secondLetterValue = array[1]; array[0] = array[0].charCodeAt(0); array[1] = array[array.length-1]; array[array.length-1] = secondLetterValue; answer.push(array.join(‘’)); }text = text.split(‘ ‘); text.forEach(function(word){ word = word.split(‘’); go(word); });return answer.join(‘ ‘);}
- I make a variable that is an empty array called answer
- I make text equal itself when it has the .split( ) method called on it with a space separating the elements in the new array to be made.
- On the array that is now text, I call the .forEach( ) method on it with word as a placeholder argument representing each element in the text array when it goes through the for loop implied by the method .forEach( ).
- Inside the code block, I make word equal itself with .split(‘’) being used on it so that it creates an array.
- Then we call the function go with the word-array we made as an argument.
- Earlier I made a function inside our function called go.
- In our new function we make a new variable called secondLetterValue = array[1]. The second element in the array argument that we pass in.
let secondLetterValue = array[1];
- I make the first character in the array argument equal itself converted into an ASCII code-character
array[0] = array[0].charCodeAt(0);
- I make the second character in the array argument equal the last character in the array argument
array[1] = array[array.length-1];
- I make the last character int he array argument equal secondLetterValue, the original value of what had been the 2nd element in the array at the start of this sub function.
- Then I push into the array answer our array argument made into a string with .join(‘’), now that all the appropriate alterations have been done.
“Best Practices Answer”
const encryptThis = text => text.split(‘ ‘).map(word => word .replace(/(^\w)(\w)(\w*)(\w$)/, `$1$4$3$2`) .replace(/^\w/, word.charCodeAt(0))) .join(‘ ‘);
So these guys are smart, smart people. They solved it in these steps:
- Like me, they used .split( ) on the text argument. We separate elements based on whether there is a space between them as normal words do.
- After this, they chain the .map( ) array function onto what is now an array and pass in the argument word to represent each element in the text array.
- for each word element in the text array, we use the string method .replace( ),
- as the first argument, we seek the
- first character => (^\w)
- second character => (\w)
- third character => (\w*)
- fourth character => (\w$)
- as the second argument, we pass in
- $1 — the first character is going to be (^\w)
- $4 — the fourth character will be in the place of where the second one should be (\w$)
- $3 — the third (\w*)
- $2 — the second character (\w)
- .replace( ) is called and we replace the first character [represetned by this RegEx expression] : /^\w/ with whatever the ASCII key for the character being referenced by the RegEx expression via the following string method word.charCodeAt(0).
- You use 0 with the .charCodeAt( ) method to get an ASCII character returned to you.
- Then we join it all together with .split( ).
That’s all folks, I gotta run!