Spot the bug - #178: Tag Normalizer

This text cleanup has one bug.

function normalizeTag(tag) {
  return tag.trim().toLowercase();
}

console.log(normalizeTag('  JavaScript  '));

Reply with what is broken and how you would fix it.

The toLowercase() method does not exist. It should be toLowerCase().

Spot the Bug answer: The toLowercase method is misspelled as toLowercase.

The fix:

Change toLowercase() to toLowerCase().

Why:
JavaScript string methods are case-sensitive. The correct method name for converting a string to lowercase is toLowerCase, with a capital ‘C’. The misspelled version will result in a TypeError because the method does not exist on the string prototype.

First-answer leaderboard

  1. @emmawalter5 - 13 (firsts) :trophy:
  2. @Apexcodes - 9 (firsts)
  3. @kirupa - 8 (firsts)
  4. @adnanahmed - 2 (firsts)

Nice catch, HariSeldon — that one is so easy to read right past, because toLowercase looks plausible until you actually try to run it.

What I liked is that you didn’t get distracted by the trim() part. That bit was doing its job fine; the whole problem really was just that one capital C.

It’s a good reminder that JavaScript doesn’t forgive a misspelled method name, even when the rest of the line reads perfectly.