Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Hi. I'm a 78-year-old IT techie but I'm new at this stuff. I coded the following:

Code:
let node = document.createElement('p');
  let linebreak = document.createElement('br');
  let textnode = document.createTextNode("noun count = " + noun_count + "; verb count = " + verb_count);
  node.appendChild(textnode);
  document.getElementById("par1").appendChild(node);
  textnode = document.createTextNode("; others count = " + adjetc_count + "; total count = " + vocab_count);
  node.appendChild(textnode);
  document.getElementById("par1").appendChild(node);

 swahili_word = swahili_word.toLowerCase();
  node.appendChild(linebreak);
  document.getElementById("par1").appendChild(node);
  textnode = document.createTextNode("Swahili word - " + swahili_word);
  node.appendChild(textnode);
  document.getElementById("par1").appendChild(node);

and it worked fine.  I then added the following:

let dict_pos = trsnoun(swahili_word);
  if (dict_pos > -1)
  {
    eng_word = nouns[dict_pos].engsing;
    node.appendChild(linebreak);
    document.getElementById("par1").appendChild(node);
    textnode = document.createTextNode("English word(s) - " + eng_word);
    node.appendChild(textnode);
    document.getElementById("par1").appendChild(node);
    return;
  }

Now the second linebreak works but the first stopped working:

noun count = 3118; verb count = 2104; others count = 524; total count = 5746Swahili word - kilima
English word(s) - hill

Can anyone tell me why?
 
Last edited by a moderator:
Hey there, The issue you're encountering is due to reusing the same node variable, causing the line breaks to be appended repeatedly to the same node. To fix this, you should create new elements for each piece of text and line break.

JavaScript:
let node = document.createElement('p');
let textnode = document.createTextNode("noun count = " + noun_count + "; verb count = " + verb_count);
node.appendChild(textnode);
document.getElementById("par1").appendChild(node);

textnode = document.createTextNode("; others count = " + adj_count + "; total count = " + vocab_count);
node.appendChild(textnode);
document.getElementById("par1").appendChild(node);

swahili_word = swahili_word.toLowerCase();
node.appendChild(document.createElement('br'));
document.getElementById("par1").appendChild(node);

textnode = document.createTextNode("Swahili word - " + swahili_word);
node.appendChild(textnode);
document.getElementById("par1").appendChild(node);

let dict_pos = trsnoun(swahili_word);
if (dict_pos > -1) {
    let linebreak = document.createElement('br');
    document.getElementById("par1").appendChild(linebreak);

    let engNode = document.createElement('p');
    let textnode = document.createTextNode("English word(s) - " + nouns[dict_pos].engsing);
    engNode.appendChild(textnode);
    document.getElementById("par1").appendChild(engNode);
    return;
}

This ensures that each line break and text node is appended correctly, maintaining your formatting. I hope this helps but im no Java coder 😀
 
Back
Top Bottom