# Spot the bug - #97: Theme Color Parser

**URL:** <https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741>\
**Category:** web dev\
**Created:** [July 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741 "2026-07-25T07:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [July 25, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741/1 "2026-07-25T07:00:07Z")

</div>

Color helper has one tiny bug.

```js
function normalizeHex(hex) {
  const value = hex.startsWith('#') ? hex : '#' + hex;
  return value.slice(0, 6);
}

console.log(normalizeHex('#12abef'));

```

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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 25, 2026, 4:57pm UTC](https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741/2 "2026-07-25T16:57:31Z")

</div>

Is it the range used by slice?

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [July 25, 2026, 4:57pm UTC](https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741/3 "2026-07-25T16:57:42Z")

</div>

Close, but the range isn’t the bug by itself. `slice(0, 6)` counts from index 0, and the `#` is sitting right there at index 0. So you’re keeping the `#` plus only 5 hex chars, not 6.

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [July 26, 2026, 9:00am UTC](https://forum.kirupa.com/t/spot-the-bug-97-theme-color-parser/682741/4 "2026-07-26T09:00:15Z")

</div>

**Spot the Bug answer:** slice(0, 6) cuts off the last hex digit because it counts the leading # as one of the 6 characters, truncating a valid 6-digit hex code to 5 digits plus the hash

**The fix:**

```js
return value.slice(0, 7);

```

**Why:**  
A normalized hex color needs the # plus 6 characters, totaling 7 characters. Slicing to 6 keeps the # and only 5 digits, so ‘#12abef’ becomes ‘#12abe’, dropping the final ‘f’.

* * *

**Nobody got this one.** It was a sneaky one.

**Close but not quite:**

@kirupa - Doesn’t identify the actual fix (slice(0,7)) or clearly explain that the # is counted in the 6 characters, just vaguely gestures at slice.
