Spot the bug - #106: 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 trim method needs an argument to specify how to trim :sweat_smile:

Trim’s fine actually, no args needed there. It just strips whitespace from both ends by default. The real bug is toLowercase(). Method’s actually toLowerCase() with a capital L.

1 Like

Spot the Bug answer: The code calls toLowercase() but the actual String method is toLowerCase (capital L), so it throws a TypeError since toLowercase is not a function.

The fix:

return tag.trim().toLowerCase();

Why:
JavaScript’s String.prototype method is named toLowerCase with a capital L, and since JS is case sensitive, calling toLowercase() looks up an undefined property and calling it throws a TypeError.


Nobody got this one. It was a sneaky one.

Close but not quite:

@kirupa - They misidentified the bug; trim() takes no arguments, the actual issue is the incorrect capitalization of toLowercase (should be toLowerCase).

First-answer leaderboard

  1. @emmawalter5 - 1 (first) :trophy:
  2. @kirupa - 1 (first) :trophy:

The bug is toLowercase(). It should be toLowerCase() (capital C).


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

ooh a guess is in ๐Ÿ‘€ answer drops later today, staying quiet till then