# Spot the bug - #134: Playlist Generator

**URL:** https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244
**Category:** web dev
**Created:** [September 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244 "2026-09-03T07:00:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)
#### Post date: [September 3, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/1 "2026-09-03T07:00:08Z")

</div>

Why is my party playlist skipping the final song?

```js
const partyTracks = [
  "Electric Disco Jelly",
  "Neon Flamingo Waltz",
  "Galactic Bagpipe Jam",
  "Midnight Kazoo Solo",
  "Cosmic Polka Fever"
];

function announceSetlist(tracks) {
  const results = [];
  // Announce each track by its slot number
  for (let i = 0; i < tracks.length - 1; i++) {
    const trackNumber = i + 1;
    const title = tracks[i];
    results.push(`Track #${trackNumber}: ${title}`);
  }
  return results;
}

const setlist = announceSetlist(partyTracks);
console.log(`Prepared ${setlist.length} tracks out of ${partyTracks.length}`);
console.log(setlist.join("\n"));

```

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

---

<div class="post-metadata">

### Author: ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)
#### Post date: [September 4, 2026, 7:20am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/2 "2026-09-04T07:20:18Z")

</div>

Yo it’s the `i < tracks.length - 1` in your loop. that ` - 1` is cutting off the last track. just change it to `i < tracks.length` to get everything.

---

<div class="post-metadata">

### Author: ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)
#### Post date: [September 4, 2026, 8:00am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/3 "2026-09-04T08:00:21Z")

</div>

**Spot the Bug answer:** The loop condition `i < tracks.length - 1` causes the last element of the array to be skipped.

**The fix:**  
Change the loop condition to `i < tracks.length` or `i <= tracks.length - 1`.

**Why:**  
Array indices are 0-based. For an array of length N, valid indices are 0 to N-1. The original condition `i < tracks.length - 1` means the loop runs for `i` from 0 up to `tracks.length - 2`, effectively missing the element at index `tracks.length - 1`.

**First-answer leaderboard**

1. @kirupa - 6 (firsts) 🏆
2. @Apexcodes - 5 (firsts)
3. @adnanahmed - 2 (firsts)
4. @emmawalter5 - 2 (firsts)

---

<div class="post-metadata">

### Author: ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)
#### Post date: [September 5, 2026, 3:40am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/4 "2026-09-05T03:40:21Z")

</div>

That’s a classic off-by-one. I’ve seen that exact mistake cause some fun at 3am trying to figure out why the last record wasn’t processing.

---

<div class="post-metadata">

### Author: ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)
#### Post date: [September 5, 2026, 3:41am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/5 "2026-09-05T03:41:54Z")

</div>

Yep, that `tracks.length - 1` is a classic. It’s an easy one to miss when you’re deep in the code.

---

<div class="post-metadata">

### Author: ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)
#### Post date: [September 5, 2026, 4:40am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/6 "2026-09-05T04:40:16Z")

</div>

Lol same

---

<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: [September 5, 2026, 8:40am UTC](https://forum.kirupa.com/t/spot-the-bug-134-playlist-generator/683244/7 "2026-09-05T08:40:19Z")

</div>

That’s how you end up with a playlist of nothing but elevator music.
