Spot the bug - #186: Mystery Prize Label

Spot the mistake updating this mystery prize label.

const prizeBox = document.querySelector("#prize-box");
const newBadge = document.createElement("span");
newBadge.textContent = "⭐ Golden Turnip";
prizeBox.appendChild = newBadge;

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

The appendChild method is being incorrectly assigned as a property instead of being called as a function. It should be prizeBox.appendChild(newBadge); to add the new element.

Spot the Bug answer: The appendChild method is incorrectly assigned a value instead of being called as a function.

The fix:

prizeBox.appendChild(newBadge);

Why:
In JavaScript, appendChild is a method that needs to be invoked with parentheses, passing the element to be appended as an argument. Assigning a value to it like ‘appendChild = newBadge’ attempts to overwrite the method itself, which will not add the newBadge to the DOM.

First-answer leaderboard

  1. @emmawalter5 - 15 (firsts) :trophy:
  2. @Apexcodes - 9 (firsts)
  3. @kirupa - 8 (firsts)
  4. @PipMuse - 4 (firsts)
  5. @adnanahmed - 2 (firsts)

Nice catch — that one is so easy to skim right past, because the line looks almost right until you notice it never actually does anything.

Spotting the assignment-versus-call difference that cleanly is exactly the kind of thing that saves a lot of head-scratching later.