0

My assignment is to make an ordered list in JavaScript using document.write(). I wrote the code to make it into a list, but it does not make it ordered. I would like to have the items numbered rather than bullet pointed. Below is the code that I have written so far:

<script>
  // Create ordered list
  document.write("<li> Reduce spending on non-necessities.</li>");
  document.write("<li> Use extra money to pay off debt. Starting with highest-interest credit cards.</li>");
  document.write("<li> Continue paying off debts until you are debt free.</li>");
  document.write("<li> Put a fixed percent of your pay aside every payday.</li>");
</script>
8
  • 1
    You need to wrap your <li>s in <ol> and </ol> Commented Aug 29, 2022 at 19:48
  • I know how to wrap it with HTML, but I am wondering if it is different with JavaScript Commented Aug 29, 2022 at 19:59
  • 2
    @MayaStrickland Simply add document.write("<ol>") at the start and close the tag the same way at the end Commented Aug 29, 2022 at 20:00
  • 6
    I really hope you are learning from someone who is knowledgeable about modern standards and approaches. Generally, document.write() is a sign of poor programming. Commented Aug 29, 2022 at 20:05
  • 1
    I'm sorry you ended up in a course this bad. Using document.write() is a bad sign already, but using it to output static HTML strings is completely pointless. Commented Aug 29, 2022 at 20:43

2 Answers 2

1

In JavaScript, there's usually more than one way to achieve a goal. Below, I show a different approach than using document.write().

Links to documentation for the APIs used below:

// Start with the steps strings in an array:
const steps = [
  'Reduce spending on non-necessities.',
  'Use extra money to pay off debt. Starting with highest-interest credit cards.',
  'Continue paying off debts until you are debt free.',
  'Put a fixed percent of your pay aside every payday.',
];

// Create a list element:
const orderedList = document.createElement('ol');

// Repeat the following code block once for every step:
for (const step of steps) {
  // Create a list item element:
  const listItem = document.createElement('li');
  // Set the text content of the list item to the step text:
  listItem.textContent = step;
  // Add the list item to the list:
  orderedList.appendChild(listItem);
}

// Add the list to the target div:
document.getElementById('steps-info').appendChild(orderedList);
<div id="steps-info">
  <h2>Steps:</h2>
</div>


A note about document.write() from MDN:

⚠️ Warning: Use of the document.write() method is strongly discouraged.

As the HTML spec itself warns:

This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. Users agents are explicitly allowed to avoid executing script elements inserted via this method. And to make matters even worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.

Therefore, avoid using document.write() — and if possible, update any existing code that is still using it.

Sign up to request clarification or add additional context in comments.

3 Comments

Definitely developers must now refrain from using document.write() as much as possible. Only in learning purposes shall it be used, if at all.
@coderboy I have been a professional corporate IT trainer for 30 years and I can tell you unequivocally, that someone who is teaching anti-patterns like this is most likely not doing it for learning purposes, they're doing it because they don't know HTML/JavaScript and are teaching from a very old lesson plan and don't know any better.
@ScottMarcus you are absolutely right.
0

Your code is in a ordered list because you did not include

    : (ordered list) element , from the beggining of the text and the end of the text.

    document.write("
  1. Reduce spending on non-necessities.
  2. "); document.write("
  3. Use extra money to pay off debt,starting with highest-interest credit cards.
  4. "); document.write("
  5. Continue paying off debts until you are debt free.
  6. "); document.write("
  7. Put a fixed percent of your pay aside every payday.
");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.