# Spot the bug - #82

**URL:** <https://forum.kirupa.com/t/spot-the-bug-82/682546>\
**Category:** web dev\
**Created:** [July 10, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-82/682546 "2026-07-10T07:00:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [July 10, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-82/682546/1 "2026-07-10T07:00:08Z")

</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:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [July 11, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-82/682546/2 "2026-07-11T07:20:17Z")

</div>

`slice(0, 6)` is the bug. It keeps the `#` and only 5 hex chars, so `#12abef` turns into `#12abe`.

Use `slice(0, 7)` instead, or strip the `#` first and add it back:

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

```

or

```auto
function normalizeHex(hex) {
  const raw = hex.replace(/^#/, '');
  return '#' + raw.slice(0, 6);
}

```

Confidence: high
