I'm trying to manipulate the DOM in my web project, and I need to remove a specific child node from an HTML element using JavaScript.

Can someone guide me on how to do this? Here's the code snippet I'm working with:

<div id="parent">
  <p>First child</p>
  <p>Second child</p>
  <p>Third child</p>
</div>

And here's what I've tried so far in my JavaScript.

<script>
 const parent = document.getElementById("parent");
 const childToRemove = parent.querySelector("p:nth-child(2)");
 parent.removeChild(childToRemove);
</script>

However, it's not working as expected. Can anyone provide a solution with an explanation of the code?