## DEV Community is a community of 641,544 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Challenge Algo #7 - Caesar Shift

Farid
Farid, 34 years old, Freelancer, Full Stack Developer, Geek/n3rd and Teacher.

Given a string, replace each its by a left rotation of three places, equivalent to a right shift of 23

### Example

For inputString = "the quick brown fox jumps over the lazy dog", the output should be caesarShift(inputString) = "qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald".

### Code:

``````const caesarShift = inputString =>{
const caesarAlphabet = {
'a':'x','b':'y','c':'z','d':'a','e':'b',
'f':'c','g':'d','h':'e','i':'f','j':'g',
'k':'h','l':'i','m':'j','n':'k','o':'l',
'p':'m','q':'n','r':'o','s':'p','t':'q',
'u':'r','v':'s','w':'t','x':'u','y':'v',
'z':'w',' ': ' '
};

let inputShifted = inputString.toLowerCase().split('');

for (let i = 0; i < inputShifted.length; i++) {
inputShifted[i] = caesarAlphabet[inputShifted[i]];
}

return inputShifted.join('');
}

console.log(caesarShift('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')) // qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald
``````

I am waiting for your idea/experience to refactor it and make it more smooth in the comment...

see ya

## Discussion (3)

Sabin Pandelovitch • Edited

Well, if you like to ES6, you can go all the way with the code you could do something like

``````const caesarShift = inputString => {
const caesarAlphabet = {
'a':'x','b':'y','c':'z','d':'a','e':'b',
'f':'c','g':'d','h':'e','i':'f','j':'g',
'k':'h','l':'i','m':'j','n':'k','o':'l',
'p':'m','q':'n','r':'o','s':'p','t':'q',
'u':'r','v':'s','w':'t','x':'u','y':'v',
'z':'w',' ': ' '
};

return [...inputString.toLowerCase()]
.map((el) => caesarAlphabet[el])
.join("");
};
``````

You could also write a function instead of the `caesarAlphabet` object.
This is not case sensitive anymore

``````const caesarShift = inputString => {
const caesarCallback = el => {
const charCode = el.charCodeAt(0);

let corr = " ";
if ((charCode > 96 && charCode < 100) || (charCode > 64 && charCode < 68)) {
corr = 23 + charCode;
}
if (charCode > 99 || (charCode > 67 && charCode < 91)) {
corr = charCode - 3;
}
return String.fromCharCode(corr);
};

return [...inputString].map(caesarCallback).join('')
};
``````

Or you could do something that will give headaches to many people

``````const cS = iS => String.fromCharCode(...[...iS].map(e => e.charCodeAt(0)).map(c => (c > 96 && c < 100) || (c > 64 && c < 68) ? c + 23 : c > 99 || (c > 67 && c < 91) ? c - 3 : c));
``````
Farid

ES6 is still ok but others can't be understood easily so there's no meaning making a script anyone understand unless than you.
Then in terms of performance mine is a little bit faster.

here is the benchmark

Sabin Pandelovitch • Edited

If you run the tests multiple times the scores change every time, some times mine is better some yours. I don't know about how reliable is that benchmark, for me the benchmark was not the issue here. As for ES6, any JS developer should be able to understand the first and second one.