Bug of the day

Find the Bug: Count words

"Count words" is one of the short JavaScript programs in Bug of the day. Each time it comes round, one line has a different bug planted in it. Below are the bugs it has carried so far. Read each broken version and find the line before you read the answer under it.

Updated 2026-12-03

Bug 1: can you find it?

function countWords(text) {
  const words = text.trim().split(/\s-/);
  return words.length;
}

The answer. Line 2: minus where plus belongs. It subtracts where it should add. The line should be:

const words = text.trim().split(/\s+/);

This bug ran on 21 November 2026 (bug 89).

Bug 2: can you find it?

function countWords(text) {
  const words = text.trim().split(/\s+/;
  return words.length;
}

The answer. Line 2: a missing closing paren. The closing paren is missing, so the call or condition never ends. The line should be:

const words = text.trim().split(/\s+/);

This bug ran on 2 December 2026 (bug 100).

Bug 3: can you find it?

function countWords(text) {
  const words = text.trim().split(/\s+/);
  words.length;
}

The answer. Line 3: a missing return. The value is worked out and never returned. The line should be:

return words.length;

This bug ran on 30 November 2026 (bug 98). 5 of 5 people in the app found it within three tries (100%).

The program as it should be

function countWords(text) {
  const words = text.trim().split(/\s+/);
  return words.length;
}

Reading the working version after the broken ones is the quickest way to train your eye: the differences are one or two characters each.

More programs to check

Frequently asked questions

What does the "Count words" program do?

It is a 4-line JavaScript program used in LoWisa's Bug of the day. The working version is shown on this page.

Where can I play today's bug?

On the Bug of the day page, free, in the browser, or in the LoWisa app.

Learn JavaScript from its free first level

LoWisa is on Google Play and the App Store.