DEV Community

loading...

Challenge Algo #7 - Caesar Shift

Farid
Farid, 34 years old, Freelancer, Full Stack Developer, Geek/n3rd and Teacher.
・1 min read

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
Enter fullscreen mode Exit fullscreen mode

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

see ya

Discussion (3)

Collapse
sabbin profile image
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("");
};
Enter fullscreen mode Exit fullscreen mode

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('')
};
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode
Collapse
farid212 profile image
Farid Author

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

Collapse
sabbin profile image
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.

Forem Open with the Forem app