Hi everybody,
I’ve just finished this code and would love to hear your feedback, wheter I overcomplicate things or should do more simple steps for better readibility etc.
Thank you very much for your time
PS: I made it more versatile on top of the challenge.
const ASCII_MIN = 33
const ASCII_MAX = 127
function rot13(str = '',num = 13,start = 65){
// generate array
const gen = (arr = []) => {
return arr = new Array(num).fill(null).map(e => {
if(start > ASCII_MAX){
start = ASCII_MIN
}
return String.fromCharCode(start++)})
}
// populate array
const A = gen()
const B = gen()
// 1. split into word array, then each word into letters array and map through each letter
// 2. if letter not found in either of A or B, then don't change it
// 3. if letter NOT found in A then pick letter from A with index from B and vice versa
// 4. join back together
return str = (() =>
str
.split(' ')
.map(word => word
.split('')
.map(letter => A.indexOf(letter) < 0 && B.indexOf(letter) < 0
? letter
: A.indexOf(letter) < 0
? A[B.indexOf(letter)]
: B[A.indexOf(letter)])
.join(''))
.join(' ')
)()
}