innertext sometimes contains more newlines than the user has pressed enters

It seems that the structure of the code panel is one <div> per line and some of the <div> contents can end with <br>. .innerText converts this to a string such that each <div> generates a \n and each <br> also generates a \n.

Rather than trying to count the number of \n, count the number of <div>.

Change your function to:

function setUpLineNumbers() {
 const assemblyCode = document.getElementById("assemblyCode");
 const numberOfLines = assembly.childNodes.length;
 let lineNumbersHTML = "";
 for (let i = 1; i <= numberOfLines; i++)
   lineNumbersHTML += i + ".<br/>";
 document.getElementById("lineNumbers").innerHTML = lineNumbersHTML;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top