Workshop › Text detective › Step 10 of 13

Shift a letter

What we are doing

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]
}
```

What it means

Your turn

Write the alphabet and shiftLetter at the top of the script, before box.

Skills: Functions

All steps

  1. Read the text
  2. Count the characters
  3. Update as you type
  4. Split into words
  5. The extra-space trap
  6. An empty box has no words
  7. Count each word
  8. Drop the punctuation
  9. The most common word
  10. Shift a letter
  11. Encode a whole text
  12. The secret line
  13. Build it yourself: crack the code