Workshop › Text detective › Step 10 of 13
Now a secret code: the Caesar cipher replaces each letter with the one 3 places later in the alphabet. First, a function that shifts one letter.
```js
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
function shiftLetter(letter, shift) {
const i = alphabet.indexOf(letter)
if (i === -1) return letter
return alphabet[(i + shift) % 26]
}
```alphabet.indexOf(letter) is the letter's position (a is 0, z is 25), or -1 if it is not a letter.% 26 is the remainder after dividing by 26: 25 + 3 = 28 becomes 2, so z turns into c.Write the alphabet and shiftLetter at the top of the script, before box.
Skills: Functions
← The most common word Encode a whole text →