# Spot the bug - #108: Traffic Light Cycle

**URL:** <https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954>\
**Category:** web dev\
**Created:** [August 6, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954 "2026-08-06T07:00:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [August 6, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/1 "2026-08-06T07:00:09Z")

</div>

Traffic light cycles forever but skips a color, why

```js
const lights = ['red', 'yellow', 'green'];
let index = 0;

function nextLight() {
  const current = lights[index];
  console.log('Now showing:', current);

  switch (current) {
    case 'red':
      index = 1;
      break;
    case 'yellow':
      index = 2;
    case 'green':
      index = 0;
      break;
  }
}

setInterval(nextLight, 1000);

```

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:** [August 6, 2026, 1:04pm UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/2 "2026-08-06T13:04:50Z")

</div>

Missing “break” in yellow causes it to jump straight to the green case.

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [August 6, 2026, 1:20pm UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/3 "2026-08-06T13:20:23Z")

</div>

ooh a guess on the table, noted. answer drops later today, gonna leave everyone hanging till then 😎

---

<div class="post-metadata">

**Author:** ![Sock](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sock/32/33811_2.png) [@Sock](https://forum.kirupa.com/u/Sock)\
**Post date:** [August 6, 2026, 5:24pm UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/4 "2026-08-06T17:24:51Z")

</div>

missing break; in yellow case?

---

<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:** [August 6, 2026, 5:40pm UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/5 "2026-08-06T17:40:13Z")

</div>

Got your entry logged, no confirmations from me though. Answer drops later today.

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [August 7, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-108-traffic-light-cycle/682954/6 "2026-08-07T08:00:15Z")

</div>

**Spot the Bug answer:** The ‘yellow’ case has no break, so it falls through into the ‘green’ case and immediately resets index to 0, skipping green entirely

**The fix:**  
case ‘yellow’: index = 2; break;

**Why:**  
Without a break statement, execution falls through to the next case block, so after setting index to 2 for yellow it immediately overwrites it to 0 from the green case, meaning green never gets displayed on its own turn.

* * *

**Got it:** @kirupa, @Sock

**First:** @kirupa 🏆

**First-answer leaderboard**

1. @kirupa - 2 (firsts) 🏆
2. @emmawalter5 - 1 (first)
