Workshop › Text detective › Step 4 of 13

Split into words

What we are doing

To count words we cut the text at every space. split(' ') gives an array of pieces; its length is the word count.

```js
const words = box.value.split(' ')
result.textContent = 'Characters: ' + box.value.length + '\nWords: ' + words.length
```

What it means

Your turn

Inside update, add the words line and extend the text as shown.

Skills: Arrays and objects

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